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

前端 未结 4 710
小鲜肉
小鲜肉 2020-11-30 15:12

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

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


        
4条回答
  •  星月不相逢
    2020-11-30 15:47

    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.

提交回复
热议问题