【发布时间】:2016-06-16 22:08:05
【问题描述】:
我正在尝试更新数据库中的一些数据,但我的代码有问题...看起来没有错误,但没有更新。
conexiune 是我的连接器类 cheltuieli 是我在数据库中的表,其余名称是列
错误在这一行:command.ExecuteNonQuery(); 当我点击按钮时出现错误 错误: MySql.Data.dll 中发生了“MySql.Data.MySqlClient.MySqlException”类型的第一次机会异常
附加信息:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的“35”、DistractieCultura '20'、Neprevazute '30'' 附近使用正确的语法
我不知道为什么……有什么想法吗?
private void button5_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text != "" & textBox2.Text != "" & textBox3.Text != "" & textBox4.Text != "" & textBox5.Text != "" & textBox6.Text != "")
{
MySqlConnection conexiune = null;
conexiune = Conector.getConnection();
MySqlCommand command = new MySqlCommand();
command.Connection = conexiune;
command.CommandText = "Update cheltuieli set Mancare = '" + textBox1.Text + "', Facturi = '" + textBox2.Text + "', Reparatii = '" + textBox3.Text + "', Altele '" + textBox4.Text + "', DistractieCultura '" + textBox5.Text + "', Neprevazute '" + textBox6.Text + "'";
command.ExecuteNonQuery();
MessageBox.Show("Actualizat!");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
Search();
}
}
catch (Exception ex)
{
}
}
【问题讨论】:
-
将您的查询放在一个字符串中并打印或记录它以检查查询是否格式正确。如果可能,还可以在替换参数后提供该示例查询。
-
About 12,700 questions on Stackoverflow 与此错误消息有关。答案几乎总是相同的 - 使用 SQL 参数。
标签: c# mysql database sql-update