2013-03-18 22 views
0

基本上,我有一個VB.NET項目,我連接到遠程MySQL服務器。如何找出什麼數據庫鏈接到某個特定的ID在VB.NET

我可以提供的憑據是「服務器IP」,「端口」,「數據庫ID」,「用戶名」和「密碼」。

非常基本的東西,除了它是給出的DB ID而不是實際的DB名稱。如何檢索鏈接到該ID的數據庫的名稱?

目前我使用這個連接字符串連接到它

connection.ConnectionString = "Host=" & server & ";port=" & port & ";user=" & uname & ";password=" & pword & ";"

注:我之所以需要能夠有這些變量是因爲我打算髮布這個工具我的一些朋友,它們有不同的MySQL服務器可以連接到不同的數據庫名稱等等。由於安全原因,並不是每個人都需要知道數據庫的名稱。

編輯:我可以做的另一件事是讓我的程序檢查服務器上的每個數據庫,並返回哪個數據庫中有一個特定的表,然後將該輸出用於我的程序的其餘部分。

回答

0

我發現了一種解決方法,雖然這可能不適用於其他MySQL數據庫。

我是這樣做的:

Public Function sqlConnectTest(ByVal server, port, instance, uname, pword) As Boolean 
    Dim connection As MySqlConnection 
    connection = New MySqlConnection() 
    connection.ConnectionString = "Host=" & server & ";port=" & port & _ 
";user=" & uname & ";password=" & pword & ";" 
    Try 
     connection.Open() 
     MessageBox.Show("Connection Opened Successfully") 
     **Dim stm As String = "SELECT TABLE_SCHEMA from information_schema.TABLES where TABLE_NAME like 'survivor'"** 
     Dim cmd As MySqlCommand = New MySqlCommand(stm, connection) 
     Dim reader As MySqlDataReader = cmd.ExecuteReader() 

     While reader.Read() 
      DBname = reader.GetString(0) 
     End While 
     reader.Close() 
     connection.Close() 
     Return True 
    Catch mysql_error As MySqlException 
     MessageBox.Show("Error Connecting to Database: " & mysql_error.Message) 
     Return False 
    Finally 
     connection.Dispose() 
    End Try 
End Function 

顯然,當你有一個INFORMATION_SCHEMA數據庫這一解決方案只會工作,包含一個表,該表顯示了服務器上的所有數據庫中的所有表。

相關問題