MySql DB更新问题

问题描述:

您好。我有一点问题。



当我尝试将数据输入表格时,我在mysql数据库中编写程序,我收到此警告。当数据库数据部分为NULL时,我收到此错误。问题不会返回空。

Hi. I have a little problem.

I wrote programs in mysql database when I try to enter data into a table, I get this warning. I get this error when the database data section is NULL. The problem does not return empty.

Quote:



错误:DBNull类型和对于字符串'='运算符未定义


Error: DBNull type and for the string '=' operator is not defined

我的更新代码:

my update code:

Dim Table As String = ini.ReadValue("COMM", "DBTable")
Dim Data As String = "CODE_NO='" + DataGridView1.Item(0, i).Value.ToString
Dim con As New MySqlConnection
Dim Comm As New MySqlCommand

Comm = New MySqlCommand("UPDATE " + Table + " SETKAY='" + Label4.Text + "'" + ", STATUS='Waiting', DATE='" + DateTime.Now.ToString + "'" + " WHERE " + Data + "'", con)



数据库表格单元状态=(NULL)(不工作)


Database Table cell status= (NULL) ( not working )

<br />
Database Table cell status= " "

(工作)







谢谢大家。但不明白每个解决方案。我想告诉你的照片。

http:// tinypic .com / view.php?pic = 2ljhqwk& s = 8#.VFaaw01yZ9A [ ^ ]

http://tinypic.com/view.php?pic=wircd5&s=8#.VFaa3U1yZ9A [ ^ ]

( Working )



Thanks everyone. But dont understand every solution. I want to tell with your pictures.
http://tinypic.com/view.php?pic=2ljhqwk&s=8#.VFaaw01yZ9A[^]
http://tinypic.com/view.php?pic=wircd5&s=8#.VFaa3U1yZ9A[^]

请不要使用字符串连接形成SQL语句;使用参数化查询。每次。特别是在循环中。



如您所见,当通过ADO.net返回NULL时,它将显示为 DBNull。值 /

如消息所示,您不能将该值与其他数据类型一起使用。

您可以测试它并替换价值更有用;一个真正的NULL,一个空字符串,无论你的应用程序有什么意义。
Please don't use string concatenation to form SQL statements; use a parameterized query. Every time. And particularly in a loop.

As you've seen, when a NULL is returned via ADO.net, it will appear as DBNull.Value/
As the message says, you can't just use that value with other datatypes.
You can test for that and replace the value with something more useful; a real NULL, an empty string, whatever makes sense in your application.


进一步解决方案1,看看 IFNULL [ ^ MySQL中的函数(T-SQL相当于 ISNULL [ ^ ])

所以你的代码片段可以是

Further to solution 1, have a look at the IFNULL[^] function in MySQL (the T-SQL equivalent of ISNULL[^])
So your code snippet could be
Dim Data As String = "IFNULL(CODE_NO,'')=@Item"





另请参考使用mysql中的参数 [ ^ ]


我不是故意的粗鲁,但来吧?看看你的代码,你显然没有看到任何错误?



A)否参数用于保护您免受 SQL注入和跨脚本编写等的侵害。 。

B)了解使用您所写语言的基本基本符号之间的区别是必须的。 + VS&

C) MySQL 的基本知识对于了解您正在做什么是必不可少的。在你的情况下;你没有。 。 。

I don't mean to come off rude, but come on? A look at your code and you clearly don't see anything wrong?

A) No Parameters are being used to protect you against SQL injection and cross scripting etc. . .
B) Knowing the difference between using the basic fundamental symbols of the language you are writing in is a must. + VS &
C) Basic knowledge of MySQL is imperative to know what you are doing. In your case; you don't. . .
Dim Query As String = "Update YourDB.my_table_name SET Value1=@V1, Value2=@V2 Where Value3=@Condition;" 
Parameters.AddWithValue("@V1", "Value Of 1")
Parameters.AddWithValue("@V2", "Value Of 2")
Parameters.AddWithValue("@Condition", 0)





然后Executenonquery()启动你的命令行动。



你可以看到关于MySQL的进一步解释答案,我也回答了。我希望它能帮助你开始朝着正确的方向前进。但在进一步研究之前,你将不得不做一些研究和研究基础知识。



Then Executenonquery() to initiate your action on your command.

You can see further explanation on MySQL covered in this Answer, also answered by me. I hope it helps get you started in the right direction. But you are going to have to do some research and study the basics before going further with this.