mysql - Combobox ValueMember 不适用于 MySQL

标签 mysql vb.net combobox

我的 ComboBox 控件显示 publishernameauthorlastname 并存储 publisheridauthorid

当我运行我的代码时,它会显示 publishernameauthorlastname 以及 publisheridValueMember em>authorid 然而,当插入查询运行时,它会尝试插入单词 _publisherid__authorid_

组合框代码:

Private Sub addbook_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    mysqlconn = New MySqlConnection
    mysqlconn.ConnectionString = "server=localhost;userid=root;database=librarydatabase;Convert Zero Datetime=True"
    Dim table As New DataTable

    Dim da As New MySqlDataAdapter("select * from publishertable", mysqlconn)

    da.Fill(table)
    ComboBox1.DataSource = New BindingSource(table, Nothing)
    ComboBox1.DisplayMember = "publishername"
    ComboBox1.ValueMember = "PublisherId"

    Dim pa As New MySqlDataAdapter("select * from authortable", mysqlconn)

    pa.Fill(table)
    ComboBox2.DataSource = New BindingSource(table, Nothing)
    ComboBox2.DisplayMember = "authorlastname"
    ComboBox2.ValueMember = "authorid"

End Sub

插入代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
    conn = New MySqlConnection
    conn.ConnectionString =
        "server=localhost;userid=root;database=librarydatabase"
    Dim reader As MySqlDataReader

    Try
        conn.Open()
        query = "SET foreign_key_checks = 0;insert into booktable(ISBNno,bookname,dateofpublication,genre,duodecimal,copies,copiesinstock,authorid,publisherid) Values('" & ISBNno.Text & "','" & Title.Text & "','" & dateofpublication.Text & "','" & genre.Text & "','" & duodecimal.Text & "','" & copies.Text & "','" & copies.Text & "','" & ComboBox2.ValueMember & "', '" & ComboBox1.ValueMember & "');SET foreign_key_checks = 1"

        command = New MySqlCommand(query, conn)
        reader = command.ExecuteReader

        MessageBox.Show(query)

        conn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        conn.Dispose()


    End Try

End Sub

最佳答案

您应该使用 SelectedValue 而不是 ValueMember

query = "SET foreign_key_checks = 0;insert into booktable(ISBNno,bookname,dateofpublication,genre,duodecimal,copies,copiesinstock,authorid,publisherid) Values('" & ISBNno.Text & "','" & Title.Text & "','" & dateofpublication.Text & "','" & genre.Text & "','" & duodecimal.Text & "','" & copies.Text & "','" & copies.Text & "','" & ComboBox2.ValueMember & "', '" & ComboBox1.ValueMember & "');SET foreign_key_checks = 1"

应该是

query = "SET foreign_key_checks = 0;insert into booktable(ISBNno,bookname,dateofpublication,genre,duodecimal,copies,copiesinstock,authorid,publisherid) Values('" & ISBNno.Text & "','" & Title.Text & "','" & dateofpublication.Text & "','" & genre.Text & "','" & duodecimal.Text & "','" & copies.Text & "','" & copies.Text & "','" & ComboBox2.SelectedValue & "', '" & ComboBox1.SelectedValue & "');SET foreign_key_checks = 1"

关于mysql - Combobox ValueMember 不适用于 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40785159/

相关文章:

php - 使用 PDO 对 null 调用成员函数 query()

c# - XML 列输出

.net - 如何检查日期时间值是否大于循环中的前一个值

wpf - 在 WPF 中的组合框中绑定(bind)

c# - comboBox.SelectedItem 问题

mysql - PDO:从包含反斜杠的数据库中选择字段

php - 未知列缺少 WHERE 子句 mysql 命令行

php - mysql - php blob 字段?

vb.net - 如何将变量分配给查询结果

Java - 场景生成器 - 切换场景后,文本字段和组合框中的用户输入消失