Refer to the Sqldatasourceenumerator category in the MSDN article http://msdn.microsoft.com/zh-cn/library/a6t1z9x2.aspx, where the application looks for SQL in the current network Server instance.
Insus.net in the demo, the found instance is displayed in the DropDownList control. First, pull a DropDownList control in. aspx:
Copy Code code as follows:
Server: <asp:dropdownlist id= "dropdownlistinstance" runat= "Server" ></asp:DropDownList>
Then write in the Page_Load event:
Copy Code code as follows:
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"));
}
}
As a result, Insus.net's laptop installed SQL Server 2012 and SQL Server 2008 R2, thus displaying two instances of SQL Server.