枚举SQLServer的实例
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
//所引入的命名空间
usingSystem.Data.Sql;
namespaceEnumSqlServer
{
publicpartialclassMainForm:Form
{
publicMainForm()
{
InitializeComponent();
}
privatevoidMainForm_Load(objectsender,EventArgse)
{
//枚举本地网络内所有可用SqlServer实例
SqlDataSourceEnumeratorinstance=SqlDataSourceEnumerator.Instance;
//返回
//----------------------------------------------------------------------------
//ServerName|服务器的名称
//----------------------------------------------------------------------------
//InstanceName|服务器实例的名称。如果服务器作为默认实例运行,则为空白
//----------------------------------------------------------------------------
//IsClustered|指示服务器是否属于群集
//----------------------------------------------------------------------------
//Version|服务器的版本(SQLServer2000为8.00.x,SQLServer2005为9.00.x)
//----------------------------------------------------------------------------
DataTabledt=instance.GetDataSources();
//绑定
cboServerList.DataSource=CreateCboDataSource(dt);
}
///<summary>
///取得ComBox的数据源
///</summary>
///<paramname="dt"></param>
///<returns></returns>
privateList<string>CreateCboDataSource(DataTabledt)
{
List<string>list=newList<string>();
foreach(DataRowdrindt.Rows)
{
//只取ServerName
list.Add(dr[0].ToString());
}
returnlist;
}
}
}