用c#怎么以连接SQL Server 2008

2024-11-22 22:16:44
推荐回答(1个)
回答(1):

SqlClientFactory sqlFInstance = SqlClientFactory.Instance;
DataTable dataSources = sqlFInstance.CreateDataSourceEnumerator().GetDataSources();
DataColumn column2 = dataSources.Columns["ServerName"];
DataColumn column = dataSources.Columns["InstanceName"];
//DataColumn column3 = dataSources.Columns["IsClustered"];
////是否集成验证,即Windows身份验证和SQL Server验证
//DataColumn column4 = dataSources.Columns["Version"];
DataRowCollection rows = dataSources.Rows;
string[] array = new string[rows.Count];
for (int i = 0; i < array.Length; i++)
{
string str2 = rows[i][column2] as string;
string str = rows[i][column] as string;
if ((str == null) || (str.Length == 0) || ("MSSQLSERVER" == str))
{
array[i] = str2;
}
else
{
array[i] = str2 + @"\" + str;
}
}
Array.Sort(array);
return array;
}
catch (System.Exception ex)
{
MessageBox.Show("获取网络内数据库服务器和实例出错!" + ex.ToString(), "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}
你可以参考下这个人写的博客,挺好的http://blog.csdn.net/mvcgo/article/details/7726227