How to set a Label.Text from a specific Column of an Access DB

冷暖自知 提交于 2020-01-16 13:12:06

问题


I have an MS Access Contacts database file, with tables [Contact] and [Email].
Each contact can have multiple email addresses.
The Emails table has a "Primary" boolean Column, I am unchecking all other "Primary" cells associated with a specific contact, so each contact can only have 1 primary email address.
That is the value I am trying to retrieve from the database and display in Label1 on a DataRepeater control.

Here is my code trying to get the primary email address from the list of email addresses for a contact:

Try
    Dim dbProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    Dim dbSource As String = Application.StartupPath & "\Data\Contacts.accdb"
    Dim con As New OleDbConnection
    Dim connString As String = dbProvider & dbSource
    con.ConnectionString = connString
    con.Open()
    Dim cmd As New OleDbCommand("SELECT [E-mail Address] FROM Email WHERE ContactID='" & BindingNavigatorPositionItem.Text & "' AND Primary=True ", con)
    Label1.Text = cmd.ExecuteScalar()
    con.Close()
Catch ex As Exception
    MessageBox.Show(ex.ToString)
End Try

The problem is the Label1.Text still shows Label1 when I run the program.
I know this is incorrect, but I am unable to find a solid example to pull a value from a specific cell and assign it to a label like I am trying to do.

Exception:

System.Data.OleDb.OleDbException (0x80040E07): Data type mismatch in criteria expression. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteScalar() at Contacts_AccessDB.Form2.ContactBindingSource_CurrentItemChanged(Object sender, EventArgs e) in C:\Users...\Contacts-AccessDB\Form2.vb:line 73

UPDATE: This answer was provided in a comment, but is advised not to use because it is not using parameters:

Try
    Dim dbProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    Dim dbSource As String = Application.StartupPath & "\Data\Contacts.accdb"
    Dim con As New OleDbConnection
    Dim connString As String = dbProvider & dbSource
    con.ConnectionString = connString
    con.Open()
    Dim cmd As New OleDbCommand("SELECT [E-mail Address] FROM Email WHERE ContactID=" & BindingNavigatorPositionItem.Text & " AND Primary=True ", con)
    Label1.Text = cmd.ExecuteScalar()
    con.Close()
Catch ex As Exception
    MessageBox.Show(ex.ToString)
End Try

And here is what I am currently doing:

Try
    Dim dbProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    Dim dbSource As String = Application.StartupPath & "\Data\Contacts.accdb"
    Dim connString As String = dbProvider & dbSource
    Using conn As New OleDbConnection(connString)
        Dim cmd As New OleDbCommand("SELECT [E-Mail Address] FROM Email WHERE ContactID=@ContactID AND Primary=True ", conn)
        cmd.Parameters.Add("@ContactID", OleDbType.Integer).Value = CInt(BindingNavigatorPositionItem.Text.Trim)
        conn.Open()
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        reader.Read()
        If reader.HasRows Then
            Label1.Text = reader.Item(0).ToString()
        End If
        conn.Close()
    End Using
Catch ex As Exception
    MessageBox.Show(ex.ToString)
End Try

回答1:


Try the following code. I changed your code to use parameters instead.

I did not check for errors in the code below but you should add some code to handle errors.

Using conn As New OleDbConnection( connString),
cmd As New OleDbCommand("SELECT [E-Mail Address] FROM Email WHERE ContactID=@ContactID AND Primary=True ", conn)
    cmd.Parameters.Add("@ContactID", OleDbType.Integer).Value = CInt(BindingNavigatorPositionItem.Text.Trim)
    conn.Open()
    Dim reader As OleDbDataReader = cmd.ExecuteReader()
    If reader.HasRows AndAlso reader.Read()
        Label1.Text = reader.Item(0).ToString()
    End If
End Using


来源:https://stackoverflow.com/questions/59554313/how-to-set-a-label-text-from-a-specific-column-of-an-access-db

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!