如何得到區域網路中的已存在的SQLServer。

來源:互聯網
上載者:User
    今天再看ActiveReport的Samples時,看到一段代碼,可以取到已存在的SQLServer。
    有必要收入Bolg: 1                int i;
 2                int _count;
 3
 4                try
 5                {
 6                    this.Cursor = Cursors.WaitCursor;
 7                    //Unmanaged SQLDMO call to retrieve the existing SQL Servers
 8                    object oNames;
 9                    Type SQLDMOApplication = Type.GetTypeFromProgID("SQLDMO.Application");
10                    object oSQLApp = Activator.CreateInstance(SQLDMOApplication);
11
12                    //If oSQLApp is null, then SQLDMO.DLL wasn't found installed to the system
13                    if (oSQLApp == null)
14                    {
15                        MessageBox.Show("SQL Data Model Objects assembly not found.");
16                        return;
17                    }
18
19                    //Get Available SQL Servers
20                    oNames = oSQLApp.GetType().InvokeMember("ListAvailableSQLServers", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod, Type.DefaultBinder, oSQLApp, null, CultureInfo.CurrentCulture);
21
22                    //Get SQL Server Count
23                    _count = Convert.ToInt32(oNames.GetType().InvokeMember("Count", BindingFlags.IgnoreCase | BindingFlags.GetProperty, Type.DefaultBinder, oNames, null, CultureInfo.CurrentCulture), CultureInfo.CurrentCulture);
24
25                    //Loop through the names and add them to the list
26                    for (i = 1; i < _count; i++)
27                    {
28                        //Add Current SQL Server Name
29                        this.listBox1.Items.Add(oNames.GetType().InvokeMember("Item", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod, Type.DefaultBinder, oNames, new object[1] { i }, CultureInfo.CurrentCulture));
30                    }
31
32                    //Set the selection to the first item in the list
33                    this.listBox1.SelectedIndex = 0;
34                }
35                catch (System.InvalidCastException ex)
36                {
37                    //SQLDMO object not found installed
38                    MessageBox.Show(ex.Message);
39                }
40                catch (System.ArgumentNullException ex2)
41                {
42                    //SQLDMO object not found installed
43                    MessageBox.Show("Problem retrieving SQL Server List");
44                }
45                finally
46                {
47                    this.Cursor = Cursors.Arrow;
48                }

    其中需要用到2個Reference:1using System.Reflection;
2using System.Globalization;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.