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

前端 未结 4 720
小鲜肉
小鲜肉 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:56

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

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

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

提交回复
热议问题