从MS Access表中检索数据

时间:2017-03-24 19:21:07

标签: vb.net winforms ms-access

我正在尝试创建一个Banking窗体作为作业。

我可以使用以下代码登录用户。它从MS Access数据库中读取:

myConnection.ConnectionString = connString
myConnection.Open()

Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Account WHERE Customer_Id = (SELECT [ID] FROM Customer WHERE Cust_Name = '" & txtName.Text & "') AND PIN =" & Convert.ToInt32(txtPin.Text) & ";", myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader

Dim userFound As Boolean = False

While dr.Read
    userFound = True
    loggedInName = txtName.Text
End While

现在,我正在尝试从帐户表中为刚刚登录的用户检索余额值。

有没有办法使用上面的代码来做到这一点?如果没有,那么最好的方法是什么?

1 个答案:

答案 0 :(得分:2)

使用DataReader的Item Property获取您的值:

While dr.Read
    userFound = True
    loggedInName = txtName.Text 

    Dim dblBalance As Double = 0.0

    'This shouldn't be a nullable field, but just in case:
    If dr.Item("Balance") <> DBNull.Value Then

        dblBalance = Convert.ToDouble(dr.Item("Balance"))

    End If
End While