我的c更新代码出了什么问题
本文关键字:什么 问题 代码 更新 我的 | 更新日期: 2023-09-27 18:00:40
我写的代码有id、年份和状态。状态是一个有4项的梳框,当我尝试运行此代码时,它一直告诉我由于UPDATE语句中的语法错误而失败,你能给我一些想法吗???
private void button3_Click(object sender, EventArgs e)
{
try
{
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data source= C:'crt_db.accdb";
conn.Open();
String year = dateTimePicker1.Text.ToString();
String status = comboBox2.Text.ToString();
String z =EPID.Text.ToString();
String my_querry = "update crtRenewald set Year='" + year + "',Status='" + status+ "' where EPID=" + z + "";
OleDbCommand cmd1 = new OleDbCommand(my_querry, conn);
cmd1.ExecuteNonQuery();
MessageBox.Show("Status updated");
}
catch (Exception ex)
{
MessageBox.Show("Failed due to" + ex.Message);
}
finally
{
conn.Close();
}
我认为UPDATE查询字符串中的引号不正确。尝试将其更改为:
String my_querry = "update crtRenewald set Year='" + year + "',Status='" + status + "' where EPID='" + z + "'";
试试这个代码,可能对你有帮助:-
我正在根据您的sqlsyntex使用参数化查询。
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +@"Data source= C:'crt_db.accdb";
conn.Open();
using (OleDbCommand cmd = conn.CreateCommand())
{
OleDbCommand scmd = new OleDbCommand("UPDATE crtRenewald SET Year=@Year, Status=@Status where @EPID=EPID", conn);
scmd.Parameters.AddWithValue("Year", dateTimePicker1.Text.ToString());
scmd.Parameters.AddWithValue("Status", comboBox2.Text.ToString());
scmd.Parameters.AddWithValue("EPID", EPID.Text.ToString());
scmd.ExecuteNonQuery();
}
conn.Close();