gpt4 book ai didi

mysql - 将数据库信息传递到已选择的行上的 Vb.net

转载 作者:行者123 更新时间:2023-11-29 21:49:36 25 4
gpt4 key购买 nike

我有一个 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 字符串的变体,例如

-“从 tbl_user WHERE username='"& txtUsername.Text & "' AND 密码 = '"& txtPassword.Text & "' "中选择用户名、密码、accesslvl

accesslvl的值都是0-3之间的整数。这样做的目的是,当返回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

关于mysql - 将数据库信息传递到已选择的行上的 Vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33776260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com