我有一个vb.net表单程序,通过与数据库的oledb连接进行链接。该数据库包含一个名为tbl_user的登录表。在程序中,用户输入他们的用户名和密码,并与表值
进行比较Dim sql As String = "SELECT * FROM tbl_user WHERE username='" & txtUsername.Text & "' AND password = '" & txtPassword.Text & "' "
Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)
sqlCom.Connection = conn
conn.Open()
Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()
但是我也希望在它检测到的行上传递另一段数据,这一列称为accesslvl。所以我试图通过它然后将它与语句进行比较。我无法弄清楚如何选择并返回accesslvl值。
尝试的事情:
Dim sql As String = "SELECT * FROM tbl_user WHERE username='" & txtUsername.Text & "' AND password = '" & txtPassword.Text & "' "
Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)
Dim accesslvl As String = "SELECT accesslvl FROM tbl_user WHERE username='" & txtUsername.Text & "' AND password = '" & txtPassword.Text & "' "
Dim accesslvlCom As New System.Data.OleDb.OleDbCommand(accesslvl)
Dim accesslvlInt As Integer
Open Database Connection
sqlCom.Connection = conn
conn.Open()
accesslvlCom = accesslvlInt
sql字符串的变体,例如
- "SELECT username,password,accesslvl FROM tbl_user WHERE username='" & txtUsername.Text & "' AND password = '" & txtPassword.Text & "' "
accesslvl
的值都是0到3之间的整数。这一点是这样的,当返回accesslvl
时,它将加载正确的ui。
答案 0 :(得分:0)
这就是我这样做的方式(希望它有所帮助!):
Imports System.Data.OleDb
Public Class Login
Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\database.mdb;")
Dim cm As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim username, password As String
Dim accesslvl As Integer
Private Sub button1_click(sender as object, e as EventArgs) Handles Button1.Click
'Opening the connection'
cn.Open()
cm.Connection = cn
cm.CommandType = CommandType.Text
cm.CommandText = "Select * from tbl_user Where username='" & txtUsername.text & "' and Password='" & txtPassword.text & "'"
'Execute command'
cm.ExecuteNonQuery()
'Close the connection'
cn.Close()
'data adapter is an oledb object which collects data from selected command'
da.SelectCommand = cm
'Then it fills the data set with that data'
da.Fill(ds)
'If there are no data(or no rows) Then'
If ds.Tables(0).Rows.Count = 0 Then
MessageBox.Show("Wrong username/password error goes here", "Error", MessageBoxButtons.OK)
Exit Sub
End If
'However if there is the string username will get the value of the Username column in the row 0 (the only row if there are no duplicates)'
username = ds.Tables(0).Rows(0).Item("Username")
'same with this one'
password = ds.Tables(0).Rows(0).Item("Password")
'so now beacuse the access is no case sensitive we must check the password. If its ok we are giving the accesslvl value to the accesslvl integer'
If txtUsername.Text = username And txtPassword.Text = password Then
accesslvl = CInt(ds.Tables(0).Rows(0).Item("accesslvl"))
MessageBox.Show("successful login message goes here", "Login", MessageBoxButtons.OK)
End If
End Sub
End Class