0
如何重新創建數據庫連接嚮導,如在Visual Studio中使用的那樣?
我基本上想在我的應用程序中做到這一點,所以我們的技術人員安裝我們的軟件更容易。到目前爲止,我製作了一個包含4個文本框的表單:服務器名稱,用戶名,密碼和用於選擇數據庫的組合框。一切都非常簡單,直到數據庫選擇代碼。微軟的嚮導非常高效,沒有等待數據庫組合框被填充的等待時間。這裏是最好的我能想出:
Private Sub cmbDatabase_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbDatabase.DropDown
Dim databaselist As New DataSet
Dim connection As SqlClient.SqlConnection
If txtServer.Text <> "" And txtLogin.Text <> "" And txtPassword.Text <> "" Then
Try
connection = New SqlClient.SqlConnection("Data Source=" + txtServer.Text + ";Initial Catalog=master;User ID=" + txtLogin.Text + ";Password=" + txtPassword.Text + ";Connect Timeout=5")
connection.Open()
Catch ex As Exception
End Try
If Not connection.State = ConnectionState.Closed Then
Dim command As New SqlClient.SqlDataAdapter("EXEC sp_databases", connection)
command.Fill(databaselist)
cmbDatabase.DataSource = databaselist.Tables(0)
cmbDatabase.DisplayMember = "DATABASE_NAME"
End If
End If
該代碼獲取數據庫就好了,但很明顯,如果有異常發生時,整個屏幕鎖起來,這可能需要一段時間,這取決於數據庫的錯誤是什麼恢復。我的問題是,我可以讓自己像微軟一樣酷嗎?
不知道關於SqlDataSourceEnumerator,我會研究一下。另外我使用.NET 3.5,所以沒有TPL對我:( – broke