Oledb更新sql语句示例

时间:2013-10-27 16:36:33

标签: sql oledb statements

con.Open();
cmd = new OleDbCommand("UPDATE staff SET StaffNr = @a, FirstName = @b, 
    Surname = @c, Email = @d, Balance = @e WHERE Email = @d", con);

cmd.Parameters.AddWithValue("@b", textBoxStaffName.Text);
cmd.Parameters.AddWithValue("@c", textBoxStaffSur.Text);
cmd.Parameters.AddWithValue("@d", textBoxStaffE.Text);
cmd.Parameters.AddWithValue("@e", double.Parse(textBoxSatffBal.Text));
cmd.Parameters.AddWithValue("@a", int.Parse(textBoxStaffNr.Text));
cmd.ExecuteNonQuery();
con.Close();

由于某种原因没有更新数据库,但代码运行

1 个答案:

答案 0 :(得分:0)

OldDb使用?作为参数,而非@x。您必须按顺序添加参数。 AddWithValue中忽略该名称(此方法来自通用超类)

con.Open();
cmd = new OleDbCommand("Update staff set StaffNr = ?, FirstName = ?, 
    Surname = ?, Balance = ? Where Email = ?", con);

cmd.Parameters.AddWithValue("@a", int.Parse(textBoxStaffNr.Text));
cmd.Parameters.AddWithValue("@b", textBoxStaffName.Text);
cmd.Parameters.AddWithValue("@c", textBoxStaffSur.Text);
cmd.Parameters.AddWithValue("@e", double.Parse(textBoxSatffBal.Text));
cmd.Parameters.AddWithValue("@d", textBoxStaffE.Text);
cmd.ExecuteNonQuery();
con.Close();