[英]Passing Database Information into Vb.net on a row that has been selected
我有一个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
的值都是accesslvl
之间的整数。这样做的目的是,当返回accesslvl
时,它将加载正确的ui。
这就是我这样做的方式(希望能有所帮助!):
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.