筆者使用一個 Console 應用程式程來說明如何使用 SMO 中的 SmoApplication.EnumAvailableSqlServers() 方法,來取得 SQL Servers 的清單。
步驟如下:
1. 使用 Visual Studio 建立一個 Console 應用程式。
2. 引用 Microsoft.SqlServer.Smo 的組件。(若您的電腦上沒有這個組件可以從微軟的網站下載)
3. 程式碼如下:
using System; using System.Linq; using System.Data; using System.Data.Sql; using Microsoft.SqlServer.Management.Smo; namespace SmoListSQLServers { class Program { static void Main(string[] args) { // 使用 SMO 取得 SQL Servers DataTable dtSQL = SmoApplication.EnumAvailableSqlServers(); foreach (DataRow row in dtSQL.Rows) { foreach (DataColumn col in dtSQL.Columns) { Console.WriteLine("{0} = {1}", col.ColumnName, row[col.Ordinal]); } Console.WriteLine(); } Console.ReadKey(); } } }
執行結果:
注恴事項:
1. 使用這個方式時,SQL Server Browser 需要啟動。
2. 防火牆的設定也會影響到是否可以取得 SQL Server 的資訊。
Hope this helps.
PingBack from http://www.codedstyle.com/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-smo-%e5%8f%96%e5%be%97%e7%b6%b2%e8%b7%af%e4%b8%8a%e7%9a%84-sql-servers/