[英]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.