要连接数据库,您可以使用以下步骤:
以下是一个简单的示例代码,演示如何连接数据库并验证用户登录信息:
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
请注意,这只是一个简单的示例代码,并且可能需要根据您的实际情况进行调整和修改。同时,需要确保您的数据库连接是安全和可靠的,以防止潜在的安全风险。