要連接數(shù)據(jù)庫,您可以使用以下步驟:
以下是一個簡單的示例代碼,演示如何連接數(shù)據(jù)庫并驗證用戶登錄信息:
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
請注意,這只是一個簡單的示例代碼,并且可能需要根據(jù)您的實際情況進(jìn)行調(diào)整和修改。同時,需要確保您的數(shù)據(jù)庫連接是安全和可靠的,以防止?jié)撛诘陌踩L(fēng)險。