我想使用C#更新MySQL中特定行的 4列。我想更新
如果某个特定字段已经在文本框中输入了值。我正在使用
以下查询。
string query = " update orderform set enrolmentexpected = " +
textBox2.Text + " stockonhand=" + textBox3.Text + " numberrequired = "
+ textBox4.Text + " where name = " + textBox1.Text + ";";
我得到一个例外,即mysql语法中有一些错误,但我无法
找到这样的。我的查询是正确的还是有一些语法错误,并且有一些方法更新多列
答案 0 :(得分:2)
你在那条线上遇到很多问题。
使用类似的东西
using(MySqlConnection cn = GetConnection())
{
cn.Open();
string queryText = "update orderform set enrolmentexpected = ?en, stockonhand=?st, numberrequired=?num where name = ?name;";
MySqlCommand cmd = new MySqlCommand(queryText, cn);
cmd.Parameters.AddWithValue("?en", textBox2.Text);
cmd.Parameters.AddWithValue("?st", textBox3.Text);
cmd.Parameters.AddWithValue("?num", textBox4.Text); // ?? require conversion to Int ???
cmd.Parameters.AddWithValue("?name", textBox1.Text);
cmd.ExecuteNonQuery();
}
答案 1 :(得分:0)
在表达式之后,您似乎忘记了逗号','
string query = " update orderform set enrolmentexpected = " +
textBox2.Text + ", stockonhand=" + textBox3.Text + ", numberrequired = "
+ textBox4.Text + " where name = " + textBox1.Text + ";";
和引用:
string query = " update orderform set enrolmentexpected = '" +
textBox2.Text + "', stockonhand= '" + textBox3.Text + "' , numberrequired = '"
+ textBox4.Text + "' where name = " + textBox1.Text + ";";
答案 2 :(得分:0)
您似乎没有使用单引号和逗号 使用此
string query = " update orderform set enrolmentexpected = '" +
textBox2.Text + "', stockonhand='" + textBox3.Text + "', numberrequired = '"
+ textBox4.Text + "' where name = '" + textBox1.Text + "';";
如果所有数据类型都是char或varchar。
,则此选项有效答案 3 :(得分:0)
string query = " update orderform set enrolmentexpected = " +
textBox2.Text + ", stockonhand=" + textBox3.Text + ", numberrequired = "
+ textBox4.Text + " where name = '" + textBox1.Text + "';"
猜猜名字是一个字符串而另外两个是