MySqlCommand Command.Parameters.Add is obsolete

心已入冬 提交于 2019-11-26 17:58:47
John Woo

try AddWithValue

command.Parameters.AddWithValue("@mcUserName", mcUserNameNew);
command.Parameters.AddWithValue("@mcUserPass", mcUserPassNew);
command.Parameters.AddWithValue("@twUserName", twUserNameNew);
command.Parameters.AddWithValue("@twUserPass", twUserPassNew);

and don't wrap the placeholders with single quotes.

string SQL = "INSERT INTO `twMCUserDB` (`mc_userName`, `mc_userPass`, `tw_userName`, `tw_userPass`) VALUES (@mcUserName, @mcUserPass, @twUserName, @twUserPass)";

Just edit/remove some code in this part

('@mcUserName', '@mcUserPass', '@twUserName', '@twUserPass')

to

(@mcUserName, @mcUserPass, @twUserName, @twUserPass)

and Add( to AddWithValue(

Please read this article, advising you against using AddWithValue:

https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/

It says basically that AddWithValue could sometimes incorrectly infer the correct type. Use Add instead.

@mcUserName has to match the mc_userName in the query ..

so your parm should be @mc_userName

David

This is VB code...

cmd.Parameters.AddWithValue("@number", 1) 'set @number as numeric
cmd.Parameters.AddWithValue("@text", "this will be a text variable") 

cmd.Parameters("@number").Value = 321  'now @number has a value
cmd.Parameters("@text").Value = "A string value" 'now @text has a value

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