要連接數據庫,您可以使用以下步驟:
以下是一個簡單的示例代碼,演示如何連接數據庫并驗證用戶登錄信息:
Imports System.Data.SqlClient
Public Class LoginForm
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Dim connectionString As String = "Data Source=YourServer;Initial Catalog=YourDatabase;User ID=YourUsername;Password=YourPassword;"
Dim conn As New SqlConnection(connectionString)
Dim cmd As New SqlCommand("SELECT * FROM Users WHERE Username = @Username AND Password = @Password", conn)
cmd.Parameters.AddWithValue("@Username", txtUsername.Text)
cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.HasRows Then
MessageBox.Show("Login successful!")
' Allow user to proceed to main form
Else
MessageBox.Show("Login failed. Please try again.")
End If
conn.Close()
End Sub
End Class
請注意,這只是一個簡單的示例代碼,并且可能需要根據您的實際情況進行調整和修改。同時,需要確保您的數據庫連接是安全和可靠的,以防止潛在的安全風險。