Combobox ValueMember不使用MySQL

Combobox ValueMember不使用MySQL,mysql,vb.net,combobox,Mysql,Vb.net,Combobox,My组合框控制显示publishername和authorlastname,并存储publisherid和authorid 当我运行代码时,它确实会显示publishername和authorlastname以及publisherid和authorid的ValueMember,但是当insert查询运行时,它会尝试插入单词\u publisherid和\u authorid 组合框代码: Private子addbook\u Load(发送方作为对象,e作为事件参数)处理MyBase.Load m

My
组合框
控制显示publishername和authorlastname,并存储publisherid和authorid

当我运行代码时,它确实会显示publishername和authorlastname以及publisherid和authorid的
ValueMember
,但是当insert查询运行时,它会尝试插入单词
\u publisherid
\u authorid

组合框代码:

Private子addbook\u Load(发送方作为对象,e作为事件参数)处理MyBase.Load
mysqlconn=新的MySqlConnection
mysqlconn.ConnectionString=“server=localhost;userid=root;database=librarydatabase;Convert Zero Datetime=True”
Dim表作为新数据表
将da设置为新的MySqlDataAdapter(“从publishertable中选择*”,mysqlconn)
da.填充(表格)
ComboBox1.DataSource=新的BindingSource(表,无)
ComboBox1.DisplayMember=“publishername”
ComboBox1.ValueMember=“PublisherId”
Dim pa作为新的MySqlDataAdapter(“从authortable中选择*”,mysqlconn)
pa.Fill(表格)
ComboBox2.DataSource=新的BindingSource(表,无)
ComboBox2.DisplayMember=“authorlastname”
ComboBox2.ValueMember=“authorid”
端接头
插入代码:

Private子按钮2\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
conn=新的MySQL连接
连接字符串=
“server=localhost;userid=root;database=librarydatabase”
作为MySqlDataReader的Dim读取器
尝试
康涅狄格州公开赛
query=“SET foreign\u key\u checks=0;插入到booktable(ISBNno、bookname、出版日期、流派、duodecimal、copies、copiesinstock、authorid、publisherid)值(“&ISBNno.Text&”、“&Title.Text&”、“&dateofpublication.Text&”、“&genre.Text&”、“&duodecimal.Text&”、“&copies.Text&”、“&copies.Text&”、”&ComboBox2.ValueMember&“,”&ComboBox1.ValueMember&“);设置外键检查=1”
command=newmysqlcommand(查询,连接)
reader=command.ExecuteReader
MessageBox.Show(查询)
康涅狄格州关闭
Catch ex作为MySqlException
MessageBox.Show(例如Message)
最后
康涅狄格州
结束尝试
端接头

您应该使用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"

如果要在数据库中保存publishername,应使用
SelectedValue
而不是
ValueMember
传递ComboBox1.SelectedText。您的代码可能容易受到SQL注入、转换错误和数据截断的影响,@PRABA这是不正确的。也就是说,对于突出显示的文本,它们将需要正常的
文本
属性。您也可以直接转到table
ComboBox2.DataSource=table
。而且,始终在
数据源
之前设置
显示成员
值成员
-谷歌why@TheShayaan如果成功了,你为什么不接受这个答案?