c#中更新语句的问题

本文关键字:问题 语句 更新 | 更新日期: 2023-09-27 18:12:03

我对c#编程相对较新,我在创建更新语句期间遇到了一个问题,我设法获得一个插入语句成功运行,但是似乎有一个错误与ID的确认,表单基本上从数据库查找,并在datagridview中显示信息,然后一旦记录被选中,信息显示到文本框中。

private void btnsave_Click(object sender, EventArgs e)
{
    dbCon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Elemental.accdb";

    dbCon.Open();
    string sql = "UPDATE tblOpenCalls SET [CallID]= @callid, [StaffID]= @staffid, [ProblemDesc] = @desc, [ProblemType] = @problemtype, [Department] = @department, [Location] = @location, [Urgency]= @urgency, [DateCreated] = @created, [DateAmend] = @amend, [AllocatedTo] = @allocate,  [Forename] = @forename, [Surname] = @surname WHERE [CallID]= $callid";
    OleDbCommand cmd = new OleDbCommand(sql, dbCon);

    cmd.Parameters.AddWithValue("@callid", txt_callid.Text);
    cmd.Parameters.AddWithValue("@staffid", txt_staffid);
    cmd.Parameters.AddWithValue("@desc", txt_problemdesc);
    cmd.Parameters.AddWithValue("@problemtype", txt_problemtype);
    cmd.Parameters.AddWithValue("@department", txt_department);
    cmd.Parameters.AddWithValue("@location", txt_location);
    cmd.Parameters.AddWithValue("@urgency", txt_urgency);
    cmd.Parameters.AddWithValue("@created", txt_created);
    cmd.Parameters.AddWithValue("@amend", txt_amended);
    cmd.Parameters.AddWithValue("@allocate", txt_allocate);
    cmd.Parameters.AddWithValue("@forename", txt_forename);
    cmd.Parameters.AddWithValue("@surname", txt_surname);
    cmd.ExecuteNonQuery();
    dbCon.Close();
    MessageBox.Show("Field Updated", "Update");
}

我得到的错误是查询表达式"[CallID]= $ CallID"中的语法错误。这可能是我遗漏的一些简单的东西,但是提前感谢您的建议。

c#中更新语句的问题

你好像打错了;

WHERE [CallID]= $callid

应该是

WHERE [CallID]= @callid