开发者

How can I re-create the database connection wizard like the one used in Visual Studio?

How can I re-create the database connection wizard like the one used in Visual Studio?

I basically want to make this in my application, so our technicians have an easier time installing our software. So far, I made a form with 4 text boxes: Server name, user name, password, and a combo-box to select a database. Everything was pretty easy until the database selection code开发者_如何学Python. Microsoft's wizard is very efficient, there's no wait time when the database combo box is being filled. Here's the best i could come up with:

  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

That code gets the databases just fine, but obviously if an exception happens the whole screen locks up and it might take a while to recover depending on what the database error is. My question is, can I make mine just as cool/efficient as Microsoft's?


Sure you can. Microsoft just took some more time. All the tools they used are also at your disposal. Here are some clues:

  1. Process as much of the database queries as you can on a Non-UI thread. For example, when using SqlDataSourceEnumerator to find all of the SQL instances on the network, put it in a thread, or even better use the TPL and make it a cancelable Task. Put as much work on a non-UI thread so it doesn't block. If it might be an operation that takes a while to return, display a dialog like "Searching" with a cancel button that cancels the Thread / Task.
  2. Better error handling. Handle exceptions and decide if they are useful to the user, or not.
  3. As far as getting databases, I'd stay away from a SqlDataAdapter and just use a reader. A Data Adapter is pretty heavyweight for such little information.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜