檢測局域網(wǎng)電腦是否有安裝SQL Server數(shù)據(jù)庫
本文主要介紹如何檢測局域網(wǎng)中的電腦是否有安裝SQL Server數(shù)據(jù)庫,并將其列出的方法。接下來我們就開始介紹這一過程的實(shí)現(xiàn)。
引用SQL DMO組件。
- //取得本局域網(wǎng)內(nèi)所有可用sql服務(wù)器名
- 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組件出錯(cuò), 則默認(rèn)把本機(jī)名寫進(jìn)去
- MessageBox.Show("無法取得服務(wù)器列表,可能是缺少SDLDMO.DLL!");
- cmbServer.Items.Add(System.Net.Dns.GetHostName());
- }
為什么我用panyee(快樂王子)的那個(gè)例子一直出現(xiàn)“無法取得服務(wù)器列表,可能是缺少SDLDMO.DLL”,我有這個(gè)文件?。?/p>
如果用“http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=BCEAADFB-CFF3-4804-B3B3-6C7D6488982B”里的例子也不行會(huì)出現(xiàn)以下信息:
"未處理的“System.InvalidCastException”類型的異常出現(xiàn)在WindowsApplication1.exe 中。
其他信息:接口SQLDMO.NameList 的QueryInterface 失敗。
怎么回事,請(qǐng)高手幫幫忙??!
***,你的SQL Server數(shù)據(jù)庫版本不夠。如果要使用SQLDMO.DLL就要去下載SQL sp2.
第二,如果你想列出局域網(wǎng)內(nèi)的所有的SQl server。建議你用Sql server自帶的 isql.exe 這個(gè)文件只要是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是一個(gè)ComoBox。
你可以現(xiàn)在cmd中輸入isql.exe -? 看看參數(shù)序列中有沒有你想要的。
至于說列出局域網(wǎng)內(nèi)的SQL Server數(shù)據(jù)庫要輸入 isql -L就可以了。
- private void cmbDatabase_Enter(object sender, System.EventArgs e)
- {
- //取得某服務(wù)器上的各個(gè)表名
- 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;
- }
這樣,我們就能檢測到局域網(wǎng)內(nèi)是否安裝有SQL Server數(shù)據(jù)庫,并能將其列出了。本文就介紹到這里,希望能對(duì)您有所幫助。
【編輯推薦】