檢測局域網電腦是否有安裝SQL Server數據庫
本文主要介紹如何檢測局域網中的電腦是否有安裝SQL Server數據庫,并將其列出的方法。接下來我們就開始介紹這一過程的實現。
引用SQL DMO組件。
- //取得本局域網內所有可用sql服務器名
- cmbServer.Items.Clear();
- try
- {
- SQLDMO.Application app = new SQLDMO.ApplicationClass();
- SQLDMO.NameList list = app.ListAvailableSQLServers();
- int iCount = list.Count;
- for(int i = 0; i < iCount; i ++)
- {
- string sTemp = list.Item(i);
- if(sTemp != null)
- cmbServer.Items.Add(sTemp);
- }
- }
- catch
- {
- //如果取得SQLDMO組件出錯, 則默認把本機名寫進去
- MessageBox.Show("無法取得服務器列表,可能是缺少SDLDMO.DLL!");
- cmbServer.Items.Add(System.Net.Dns.GetHostName());
- }
為什么我用panyee(快樂王子)的那個例子一直出現“無法取得服務器列表,可能是缺少SDLDMO.DLL”,我有這個文件啊!
如果用“http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=BCEAADFB-CFF3-4804-B3B3-6C7D6488982B”里的例子也不行會出現以下信息:
"未處理的“System.InvalidCastException”類型的異常出現在WindowsApplication1.exe 中。
其他信息:接口SQLDMO.NameList 的QueryInterface 失敗。
怎么回事,請高手幫幫忙啊!
***,你的SQL Server數據庫版本不夠。如果要使用SQLDMO.DLL就要去下載SQL sp2.
第二,如果你想列出局域網內的所有的SQl server。建議你用Sql server自帶的 isql.exe 這個文件只要是sql server 6.5以上就可以了。
下面是源碼:
- string fileName = "C:\\Program Files\\Microsoft SQL Server\\80\\Tools\\Binn\\isql.exe";
- if(System.IO.File.Exists(fileName))
- {
- System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo(fileName,"-L");
- processStartInfo.UseShellExecute = false;
- processStartInfo.CreateNoWindow = true;
- processStartInfo.RedirectStandardOutput = true;
- processStartInfo.RedirectStandardError = true;
- System.Diagnostics.Process process = System.Diagnostics.Process.Start(processStartInfo);
- process.WaitForExit();
- cboServerList.Items.Clear();
- int line = 1;
- string server = null;
- while(process.StandardOutput.Peek() > -1)
- {
- server = process.StandardOutput.ReadLine().Trim();
- line +=1;
- if ( line > 6)
- {
- cboServerList.Items.Add(server);
- }
- server = null;
- }
- }
- cboServerList.Items.Remove(System.Environment.MachineName);
- cboServerList.Items.Add("localhost");
cboServerList是一個ComoBox。
你可以現在cmd中輸入isql.exe -? 看看參數序列中有沒有你想要的。
至于說列出局域網內的SQL Server數據庫要輸入 isql -L就可以了。
- private void cmbDatabase_Enter(object sender, System.EventArgs e)
- {
- //取得某服務器上的各個表名
- string strServer = cmbServer.Text;
- string strUid = txtUid.Text;
- if(strServer.Trim() != "" && strUid.Trim() != "")
- {
- string strPwd = txtPwd.Text;
- string strConn = "server=" + strServer + ";database=master;uid=" + strUid + ";pwd=" + strPwd;
- SqlConnection conn = null;
- try
- {
- conn = new SqlConnection(strConn);
- string strSQL = "select * from sysdatabases order by dbid";
- SqlDataAdapter cmd = new SqlDataAdapter(strSQL, conn);
- DataSet ds = new DataSet();
- cmd.Fill(ds, "Databases");
- cmbDatabase.Items.Clear();
- for(int i = 0; i < ds.Tables["Databases"].Rows.Count; i ++)
- {
- string strDb = ds.Tables["Databases"].Rows[i]["name"].ToString();
- cmbDatabase.Items.Add(strDb);
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- finally
- {
- if(conn.State == ConnectionState.Open)
- conn.Close();
- }
- }
- this.Cursor = Cursors.Default;
- }
這樣,我們就能檢測到局域網內是否安裝有SQL Server數據庫,并能將其列出了。本文就介紹到這里,希望能對您有所幫助。
【編輯推薦】