VB.NET - 从Access数据库中的字段获取值

时间:2014-07-21 09:32:36

标签: vb.net ms-access

我的游戏根据已登录的用户将分数保存到数据库字段中。如何从字段中检索分数值并将其输出到文本框?登录的用户显示在文本框中。允许用户登录的代码如下所示。我需要在frmGame.Show()之前检索得分值。 感谢您的帮助

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

    'Check if username or password fields are empty
    If txtUsername.Text = "" Or txtPassword.Text = "" Then
        MessageBox.Show("Please complete the required fields", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        'Declare connection to database
        'OleDb = Object Linking and Embedding, Database
        Dim connDB As New System.Data.OleDb.OleDbConnection()
        'Location of Access Database
        connDB.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Year 12\Information Technology\Programming\WindowsApplication2\userDatabase.accdb"

        Try
            'Holds the username and password 
            Dim sql As String = "SELECT * FROM tbl_user WHERE User='" & txtUsername.Text & "' AND Pass= '" & txtPassword.Text & "'"
            Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)

            'Open database connection
            sqlCom.Connection = connDB
            connDB.Open()
            Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()

            'If username and password validated then display game form
            If sqlRead.Read() Then
                Me.Hide()
                frmGame.Show()
            Else
                'Authentication failure message 
                MessageBox.Show("Incorrect username or password", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtUsername.Text = ""
                txtPassword.Text = ""
                txtUsername.Focus()
            End If

            'Database connection failure message 
        Catch ex As Exception
            MessageBox.Show("Connection to User Database failed" & ex.Message, "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End If

End Sub

0 个答案:

没有答案