using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.windows.Forms;
//所引入的命名空间
using System.Data.Sql;
namespace EnumSqlServer
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
//枚举本地网络内所有可用SqlServer实例
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
//返回
//----------------------------------------------------------------------------
// ServerName|服务器的名称
//----------------------------------------------------------------------------
// InstanceName|服务器实例的名称。如果服务器作为默认实例运行,则为空白
//----------------------------------------------------------------------------
// IsClustered|指示服务器是否属于群集
//----------------------------------------------------------------------------
// Version|服务器的版本(SQLServer2000为8.00.x,SQLServer2005为9.00.x)
//----------------------------------------------------------------------------
DataTable dt = instance.GetDataSources();
//绑定
cboServerList.DataSource = CreateCboDataSource(dt);
}
/// <summary>
/// 取得ComBox的数据源
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private List<string> CreateCboDataSource(DataTable dt)
{
List<string> list = new List<string>();
foreach (DataRow dr in dt.Rows)
{
//只取ServerName
list.Add(dr[0].ToString());
}
return list;
}
}
}