我正在做一個C#Windows窗體應用程序在Visual Studio 2010的MySqlCommand Command.Parameters.Add是過時
即應用程序連接到一個MySQL數據庫,我想在裏面插入數據。
現在做我有這部分代碼:
MySqlConnection connection;
string cs = @"server=server ip;userid=username;password=userpass;database=databse";
connection = new MySqlConnection(cs);
connection.Open();
MySqlCommand command = new MySqlCommand();
string SQL = "INSERT INTO `twMCUserDB` (`mc_userName`, `mc_userPass`, `tw_userName`, `tw_userPass`) VALUES ('@mcUserName', '@mcUserPass', '@twUserName', '@twUserPass')";
command.CommandText = SQL;
command.Parameters.Add("@mcUserName", mcUserNameNew);
command.Parameters.Add("@mcUserPass", mcUserPassNew);
command.Parameters.Add("@twUserName", twUserNameNew);
command.Parameters.Add("@twUserPass", twUserPassNew);
command.Connection = connection;
command.ExecuteNonQuery();
connection.Close();
連接是好的。這樣可行。
我試過here,我現在的做法是保存查詢的方式。這還是對的嗎?
而現在到了真正的問題。通過上面的代碼,我得到Visual Studio中的以下警告:
'MySql.Data.MySqlClient.MySqlParameterCollection.Add(string, object)' is obsolete: '"Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value)"'
這一警告是每parameters.add
而且它甚至沒有工作,因爲插入的值是@mcUserName ,@mcUserPass等等,而不是變量mcUserNameNew等等值持有...
所以我的問題是,我做錯了什麼,什麼是新的方式來注入sql保存做一個查詢?
使用'AddWithValue'作爲警告 – Habib
@Habib建議,完成,警告都沒有了,但仍然沒有在數據庫中正確的價值...] – Mathlight