我对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'中的语法错误。这可能是我错过的一些简单的事情,但提前感谢您的建议。
答案 0 :(得分:8)
你似乎有一个错字;
WHERE [CallID]= $callid
应该很可能是
WHERE [CallID]= @callid