【发布时间】:2014-02-06 04:22:26
【问题描述】:
此程序要求用户从组合框中选择他们的卡号,然后输入他们的密码。如果他们的密码与数据库中的密码匹配,那么他们就会登录。问题是,我的代码没有选择一个特定的用户,它只需要数据库中的第一个用户,我无法登录任何其他用户,因为我没有不知道如何编写选择特定用户的代码。
这是我到目前为止所写的。我认为问题出在 RetriveAccountInformation() 函数中。在那里的某个地方我需要代码来指定它检索在 cboAccountNumbers 组合框中选择的 CardNumber 的数据。任何帮助表示赞赏。
导入 MySql.Data
导入 MySql.Data.MySqlClient
公开课表1
Dim dbCon As MySqlConnection
Dim strQuery As String = ""
Dim SQLcmd As MySqlCommand
Dim DataReader As MySqlDataReader
Private m_strPass As String
Private m_decBalance As Decimal
Private m_strName As String
Private m_strUserPass As String
Private m_strCardNumber As String
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
'Assign users guessed password to variable
m_strUserPass = txtPass.Text
RetrieveAccountInformation() ' invoke
' determine if PIN number is within valid range
If m_strUserPass = m_strPass Then
lblWelcome.Text = "Hi"
Else
' indicate that incorrect password was provided
lblWelcome.Text = "Sorry, Password the is incorrect." _
& "Please re-enter the password ."
' clear user's previous PIN entry
m_strUserPass = ""
End If
txtPass.Clear() ' clear TextBox
End Sub
' load application Form
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Prepare connection and query
Try
dbCon = New MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd=mysql")
strQuery = "SELECT CardNumber " &
"FROM Account"
SQLcmd = New MySqlCommand(strQuery, dbCon)
'Open the connection
dbCon.Open()
' create database reader to read information from database
DataReader = SQLcmd.ExecuteReader
' fill ComboBox with account numbers
While DataReader.Read
cboAccountNumbers.Items.Add(DataReader("CardNumber"))
End While
'Close the connection
DataReader.Close()
dbCon.Close()
Catch ex As Exception
'Output error message to user with explaination of error
MsgBox("Failure to communicate" & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
' invoke when user provides account number
Private Sub RetrieveAccountInformation()
' specify account number of record from which data
' will be retrieved
dbCon = New MySqlConnection("Server=localhost;Database=***;Uid=***;Pwd=***")
strQuery = "SELECT Password, Name, Balance " &
"FROM Account"
SQLcmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open() ' open database connection
' create database reader to read information from database
DataReader = SQLcmd.ExecuteReader
DataReader.Read() ' open data reader connection
' retrieve PIN number, balance amount and first name
' information from database
m_strPass = Convert.ToString(DataReader("Password"))
m_decBalance = Convert.ToDecimal(DataReader("Balance"))
m_strName = Convert.ToString(DataReader("Name"))
DataReader.Close() ' close data reader connection
dbCon.Close() ' close database connection
End Sub ' RetrieveAccountInformation
结束类
【问题讨论】:
-
您需要在选择查询中添加 WHERE 条件
标签: mysql sql vb.net database-connection database-table