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;
}
}
}