单击按钮连接 SQL 服务器 VB.net

Connect SQL Server on button click VB.net

如何在单击按钮时连接 SQL 服务器?
我想在文本框中手动输入服务器、用户名和密码。

我这里有一个代码,但我不确定它是否正确。

Private Sub SQLConnect_Click(sender As Object, e As EventArgs) Handles SQLConnect.Click
    Dim con As SqlConnection = New SqlConnection("Server=" & SQLServer.Text & ";Database=GameDB;User Id=" & SQLUser.Text & ";Password=" & SQLPwd.Text & ";")
    con.Open()
        MsgBox("Successfully Connected!")
    'con.Close()
End Sub

不知道如何检查连接是否正确。如果输入正确或错误。
请给我完整的代码示例。谢谢!

Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim connetionString As String
    Dim cnn As SqlConnection
    connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
    cnn = New SqlConnection(connetionString)
    Try
        cnn.Open()
        MsgBox("Connection Open ! ")
        cnn.Close()
    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try
End Sub
End Class
Dim builder As New SqlConnectionStringBuilder()
builder.DataSource = SQLServer.Text
builder.InitialCatalog = "GameDB"
builder.UserID = SQLUser.Text
builder.Password = SQLPwd.Text
builder.ApplicationName = "MyGame"

Using connection As New SqlConnection(builder.ConnectionString)
   Try
       connection.Open()
       MsgBox("Connection can be opened")
   Catch ex As Exception
       MsgBox("Connection can not be opened: " & vbCrLf & ex)
   End Try
End Using