如何得到局域网中的已存在的SQLServer。

    今天再看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:
1 using  System.Reflection;
2 using  System.Globalization;

转载于:https://www.cnblogs.com/samcn/archive/2008/05/31/1211285.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值