如何列出在C#中使用SMO的SQL Server的可用实例? [英] How to list available instances of SQL Servers using SMO in C#?

查看:514
本文介绍了如何列出在C#中使用SMO的SQL Server的可用实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释我什么,我错了,我做了下面这段code:

Can anybody explain me what I wrong I am doing in the following piece of code:

DataTable dt=SmoApplication.EnumAvailableSqlServer(true);
Server sr = new Server("Test");

foreach(DataBase db in sr.DataBases)
{
    Console.WriteLine(db["name"]);
}

这给出了 sr.Databases 的异常无法连接。

推荐答案

看看下面的链接,他们可能会有所帮助:

Take a look at the following links they may be helpful:

  • Enumerate SQL Server Instances in C#, Using ODBC
  • How to get a list of available SQL Servers using C# (MSDN)
  • Populating a list of SQL Servers

另外,你可以改变你的code到这一点:

Alternatively you could change your code to this:

DataTable dt = SmoApplication.EnumAvailableSqlServers(false);
if (dt.Rows.Count > 0)
{
    foreach (DataRow dr in dt.Rows)
    {
        Console.WriteLine(dr["Name"]);
    }
}

希望这能解决你的问题。

Hope this solves your problem.

这篇关于如何列出在C#中使用SMO的SQL Server的可用实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆