我对 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'中的语法错误。这可能是我错过的一些简单的事情,但提前感谢您的建议。