如何在 C# 中使用 SMO 列出 SQL Server 的可用实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1130580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:56:02  来源:igfitidea点击:

How to list available instances of SQL Servers using SMO in C#?

c#.netsql-serversmo

提问by

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"]);
}

It gives an exception in sr.Databasesthat can not be connected.

它给出了sr.Databases无法连接的例外。

回答by Kredns

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

查看以下链接,它们可能会有所帮助:

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.

希望这能解决您的问题。

回答by adrianbanks

Do you have a SQL Server with the instance name Test? If not, that is your problem.

您有实例名称为Test的 SQL Server吗?如果没有,那是你的问题。

It looks like you are trying to enumerate all of the local SQL Server instances. If so, this code will work:

看起来您正在尝试枚举所有本地 SQL Server 实例。如果是这样,此代码将起作用:

DataTable dt = SmoApplication.EnumAvailableSqlServers(true);

foreach (DataRow dr in dt.Rows)
{
    Console.WriteLine(dr["Name"]);
    Console.WriteLine("   " + dr["Server"]);
    Console.WriteLine("   " + dr["Instance"]);
    Console.WriteLine("   " + dr["Version"]);
    Console.WriteLine("   " + dr["IsLocal"]);
}

回答by Ganesh R.

Just in case the question is titled wrong i.e. he wants to find the databases in the particular instance:

以防万一问题标题错误,即他想在特定实例中找到数据库:

using System;
using Microsoft.SqlServer.Management.Smo;
using System.Data;
using System.Windows.Forms;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Server sr = new Server("MACHINE_NAME\INSTANCE_NAME");

            try
            {
                foreach (Database db in sr.Databases)
                {
                    Console.WriteLine(db.Name);
                }
                Console.Read();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
            }
        }
    }
}

Else Lucas Aardvark answer is most appropriate.

Else Lucas Aardvark 的回答是最合适的。

回答by Xorsat

using Microsoft.Win32;

       RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
        String[] instances = (String[])rk.GetValue("InstalledInstances");
        if (instances.Length > 0)
        {
            foreach (String element in instances)
            {
               Console.WriteLine(element);    // element is your server name                
            }
        }