天天看點

解析c#得到區域網路内所有sqlserver資料庫執行個體

官方的做法是這樣的:

using System.Data.Sql;  

class Program  

{  

  static void Main()  

  {  

    // Retrieve the enumerator instance and then the data.  

    SqlDataSourceEnumerator instance =  

      SqlDataSourceEnumerator.Instance;  

    System.Data.DataTable table = instance.GetDataSources();  

    // Display the contents of the table.  

    DisplayData(table);  

    Console.WriteLine("Press any key to continue.");  

    Console.ReadKey();  

  }  

  private static void DisplayData(System.Data.DataTable table)  

    foreach (System.Data.DataRow row in table.Rows)  

    {  

      foreach (System.Data.DataColumn col in table.Columns)  

      {  

        Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);  

      }  

      Console.WriteLine("============================");  

    }  

請看遇到的問題及解決方法:

實際上問題就是,得到的結果隻有伺服器名字,但由于是預設執行個體,是以并沒有執行個體名字。而且,假如安裝的是sqlserver,則連接配接資料庫是必須是 伺服器\sqlexpress(預設執行個體名稱);假如安裝的是完整版的sqlexpress,則隻需 伺服器 即可連接配接。這就造成了不少問題。 上邊百度給出比較好的解決方法。

本文轉自 huohe2009 51CTO部落格,原文連結:http://blog.51cto.com/zhaojie/932275