[ORM]利用SqlDataSourceEnumerator获得局域网内的所有SQL Server服

原来用SQLDMO写了个获得服务器列表的函数,结果只能获得到SQL Server2000的服务器信息,甚至本地的SQL Server2005的信息都获得不到,觉得奇怪于是去google一下,发现了老外用VB.NET写的利用System.Data.Sql.SqlDataSourceEnumerator 获得所有在网的数据库服务器实例的信息代码如下:
地址:http://www.geckoware.com.au/codesnippets/ListSQLServers.txt

Module ListServers
Public Function StServerList() As System.Collections.Generic.List(Of SqlServerInstance)
Dim clsEnumerator As System.Data.Sql.SqlDataSourceEnumerator = System.Data.Sql.SqlDataSourceEnumerator.Instance
Dim clsDataTable As DataTable = clsEnumerator.GetDataSources()
Dim clsServers As New System.Collections.Generic.List(Of SqlServerInstance)
If clsDataTable.Rows.Count > 0 Then
For Each clsRow As DataRow In clsDataTable.Rows
clsServers.Add(New SqlServerInstance(clsRow("ServerName").ToString, clsRow("InstanceName").ToString, clsRow("IsClustered").ToString, clsRow("Version").ToString))
Next clsRow
End If
Return clsServers
End Function
Public Class SqlServerInstance
Private m_sName As String = ""
Private m_sInstance As String = ""
Private m_sClustered As String = ""
Private m_sVersion As String = ""
Public Function ServerName() As String
Return IIf(String.IsNullOrEmpty(m_sInstance), m_sName, m_sName & "\" & m_sInstance).ToString()
End Function
Public Overrides Function ToString() As String
Return m_sName & "\" & m_sInstance & "(" & m_sVersion & ")"
End Function
Public Sub New(ByVal Name As String, ByVal Instance As String, ByVal Clustered As String, ByVal Version As String)
m_sName = Name
m_sInstance = Instance
m_sClustered = IIf(String.IsNullOrEmpty(Clustered), "(Unknown)", Clustered).ToString()
m_sVersion = IIf(String.IsNullOrEmpty(Version), "(Unknown)", Version).ToString()
End Sub
Public ReadOnly Property Name() As String
Get
Return m_sName
End Get
End Property
Public ReadOnly Property InstanceName() As String
Get
Return IIf(String.IsNullOrEmpty(m_sInstance), "Default", m_sInstance).ToString()
End Get
End Property
Public ReadOnly Property Clustered() As String
Get
Return m_sClustered
End Get
End Property
Public ReadOnly Property Version() As String
Get
Return m_sVersion
End Get
End Property
End Class
End Module

于是我用C#重写了一下,代码如下:
类定义:

public class SqlServerInstance
{
private string serverName="";
private string instanceName="";
private string isClusted="";
private string version="";
public SqlServerInstance(string serverName, string instanceName, string isClusted, string version)
{
this.serverName = serverName;
this.instanceName = instanceName;
this.isClusted = isClusted;
this.version = version;
}
}

调用代码:

public void GetServers()
{
SqlDataSourceEnumerator dsEnumerator = SqlDataSourceEnumerator.Instance;
DataTable dtable = dsEnumerator.GetDataSources();
List<SqlServerInstance> ls = new List<SqlServerInstance>();
if (dtable.Rows.Count > 0)
{
cmbServer.Items.Clear();
foreach (DataRow row in dtable.Rows)
{
ls.Add(new SqlServerInstance(row["ServerName"].ToString(),row["InstanceName"].ToString(),row["IsClustered"].ToString(),row["Version"].ToString()));
}
}
}
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏