ASP.NET获取MS SQL Server安装实例

参考MSDN的文章 http://msdn.microsoft.com/zh-cn/library/a6t1z9x2.aspx 中所指的SqlDataSourceEnumerator类别,是应用程序在当前的网络中查找SQL Server实例。 

Insus.NET在演示中,是把找到的实例显示于DropDownList控件中。首先在.aspx拉一个DropDownList控件:

View Code
Server: <asp:DropDownList ID="DropDownListInstance" runat="server"></asp:DropDownList>


然后在Page_Load事件写:

View Code
protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dataTable = SqlDataSourceEnumerator.Instance.GetDataSources();

        foreach (DataRow dr in dataTable.Rows)
        {
            if (string.IsNullOrEmpty(dr["InstanceName"].ToString()))
               this.DropDownListInstance.Items.Add(string.Concat(dr["ServerName"]));
            else
               this.DropDownListInstance.Items.Add(string.Concat(dr["ServerName"], "\\", dr["InstanceName"]));
        }
    }


运行结果,Insus.NET的手提电脑安装了SQL Server 2012和SQL Server 2008 R2,因此显示两个SQL Server实例。

posted @ 2013-01-28 21:43  Insus.NET  阅读(1427)  评论(0编辑  收藏