Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersDimphOneDriveDesktopRichfield BSC IT --- 1st yearSecond SemesterProgramming 512AssignmentWinFormsApp1WinFormsApp1binDebugnet6.0-windowsDatabase2.accdb")
'
Private Sub btnLogin(sender As Object, e As EventArgs) Handles Button1.Click
If txtUsername.Text = Nothing Or txtPassword.Text = Nothing Then
MsgBox("Enter Credentials", MsgBoxStyle.Exclamation)
Else
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
Dim cmd As New OleDbCommand("select count(*) from User where UserName=? and Password=?", connection)
cmd.Parameters.AddWithValue("@1", OleDbType.VarChar).Value = txtUsername.Text
cmd.Parameters.AddWithValue("@2", OleDbType.VarChar).Value = txtPassword.Text
Dim count = Convert.ToInt32(cmd.ExecuteScalar())
If (count > 0) Then
SplashSCreen.Show()
Else
MsgBox("Account not found on Database")
End If
End If
End Sub
Private Sub btnReset(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub btnSignUp(sender As Object, e As EventArgs) Handles Button3.Click
Form2.Show()
Me.Hide()
End Sub
Private Sub btnClose(sender As Object, e As EventArgs) Handles Button4.Click
Me.Close()
End Sub
End Class
User
和Password
都是保留字,将它们用作对象名称可能会混淆数据库引擎。如果不能重命名它们,可以在SQL中将它们括起来,这样数据库引擎就可以理解你指的是对象。
select count(*) from [User] where UserName=? and [Password]=?
或者你可以用更少的括号:
select count(*) from [User] AS u where u.UserName=? and u.Password=?