String updateQuery = "UPDATE " + tableEmployeeContact + " SET LastUpdate='@CurrentTimeStamp', EmployeeLastName='@EmployeeLastName', EmployeeFirstName='@EmployeeFirstName',EmployeeAddress1='@EmployeeAddress1',EmployeeAddress2='@EmployeeAddress2',EmployeeCity='@EmployeeCity',EmployeeState='@EmployeeState',EmployeeZip='@EmployeeZip',EmployeeHomePhone='@EmployeeHomePhone',EmployeeCellPhone='@EmployeeCellPhone',EmployeeHomeEmail='@EmployeeHomeEmail',EmergencyContactName='@EmergencyContactName',EmergencyContactRelationship='@EmergencyContactRelationship',EmergencyContactHomePhone='@EmergencyContactHomePhone',EmergencyContactCellPhone='@EmergencyContactCellPhone',EmergencyContactWorkPhone='@EmergencyContactWorkPhone' WHERE EmployeeLastName='@EmployeeLastName' AND EmployeeFirstName='@EmployeeFirstName'";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(updateQuery, connection);
connection.Open();
if(connection.State == ConnectionState.Open)
{
command.Parameters.Add("@CurrentTimeStamp", OleDbType.VarChar).Value = labelTimeStamp.Text.Trim();
command.Parameters.Add("@EmployeeLastName", OleDbType.VarChar).Value = cm.LastName;
command.Parameters.Add("@EmployeeFirstName", OleDbType.VarChar).Value = cm.FirstName;
command.Parameters.Add("@EmployeeAddress1", OleDbType.VarChar).Value = cm.EmployeeAddress1;
command.Parameters.Add("@EmployeeAddress2", OleDbType.VarChar).Value = cm.EmployeeAddress2;
command.Parameters.Add("@EmployeeCity", OleDbType.VarChar).Value = cm.EmployeeCity;
command.Parameters.Add("@EmployeeState", OleDbType.VarChar).Value = cm.EmployeeState;
command.Parameters.Add("@EmployeeZip", OleDbType.VarChar).Value = cm.EmployeeZip;
command.Parameters.Add("@EmployeeHomePhone", OleDbType.VarChar).Value = cm.EmployeeHomePhone;
command.Parameters.Add("@EmployeeCellPhone", OleDbType.VarChar).Value = cm.EmployeeCellPhone;
command.Parameters.Add("@EmployeeHomeEmail", OleDbType.VarChar).Value = cm.EmployeeHomeEmail;
command.Parameters.Add("@EmergencyContactName", OleDbType.VarChar).Value = cm.EmployeeECName;
command.Parameters.Add("@EmergencyContactRelationship", OleDbType.VarChar).Value = cm.EmployeeECRelationship;
command.Parameters.Add("@EmergencyContactHomePhone", OleDbType.VarChar).Value = cm.EmployeeECHomePhone;
command.Parameters.Add("@EmergencyContactCellPhone", OleDbType.VarChar).Value = cm.EmployeeECCellPhone;
command.Parameters.Add("@EmergencyContactWorkPhone", OleDbType.VarChar).Value = cm.EmployeeECWorkPhone;
try
{
command.ExecuteNonQuery();
MessageBox.Show("Record Updated");
connection.Close();
} catch (OleDbException ex)
{
Console.WriteLine(ex.Source);
MessageBox.Show(ex.Source);
}
}
}
我收到消息框说"记录已更新" 但是在应用程序运行后,我检查了数据库并且记录没有更新。
不确定为什么它不起作用。我确保每个增值都是正确的。
当我使用此更新查询并注释if语句
下的所有参数时String updateQuery = "UPDATE " + tableEmployeeContact + " SET LastUpdate = '" + labelTimeStamp.Text.Trim() + "', EmployeeLastName = '" + cm.LastName + "', EmployeeFirstName = '" + cm.FirstName + "', EmployeeAddress1 = '" + cm.EmployeeAddress1 + "', EmployeeAddress2 = '" + cm.EmployeeAddress2 + "', EmployeeCity = '" + cm.EmployeeCity + "', EmployeeState = '" + cm.EmployeeState + "', EmployeeZip = '" + cm.EmployeeZip + "', EmployeeHomePhone = '" + cm.EmployeeHomePhone + "', EmployeeCellPhone = '" + cm.EmployeeCellPhone + "', EmployeeHomeEmail = '" + cm.EmployeeHomeEmail + "', EmergencyContactName = '" + cm.EmployeeECName + "', EmergencyContactRelationship = '" + cm.EmployeeECRelationship + "', EmergencyContactHomePhone = '" + cm.EmployeeECHomePhone + "', EmergencyContactCellPhone = '" + cm.EmployeeECCellPhone + "', EmergencyContactWorkPhone = '" + cm.EmployeeECWorkPhone + "' WHERE EmployeeLastName='" + cm.LastName + "' AND EmployeeFirstName='" + cm.FirstName + "'";
查询有效,一切都得到更新。但是我被告知应该在SQL字符串中直接使用参数而不是值。
答案 0 :(得分:1)
请参阅OleDbCommand.Parameters Property,查询中的参数是位置且未命名,因此顺序是cricital,必须在Parameters集合和查询之间匹配。使用?
作为占位符。您仍然可以在集合中命名参数,尽管在对Ms Access执行时名称没有意义。
string updateQuery = "UPDATE " + tableEmployeeContact + @" SET
LastUpdate=?
,EmployeeAddress1=?
,EmployeeAddress2=?
,EmployeeCity=?
,EmployeeState=?
,EmployeeZip=?
,EmployeeHomePhone=?
,EmployeeCellPhone=?
,EmployeeHomeEmail=?
,EmergencyContactName=?
,EmergencyContactRelationship=?
,EmergencyContactHomePhone=?
,EmergencyContactCellPhone=?
,EmergencyContactWorkPhone=?
WHERE EmployeeLastName=? AND EmployeeFirstName=?";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(updateQuery, connection);
connection.Open();
if(connection.State == ConnectionState.Open)
{
// In MS Access the order of parameters is critical!!
// I recommend replacing this with the below commented out parameter and changing your data type in MS Access accordingly. The type system is in place for a reason, not everything is a string.
command.Parameters.Add("@CurrentTimeStamp", OleDbType.VarChar).Value = labelTimeStamp.Text.Trim();
// command.Parameters.Add("@CurrentTimeStamp", OleDbType.DBTimeStamp).Value = DateTime.Now;
command.Parameters.Add("@EmployeeAddress1", OleDbType.VarChar).Value = cm.EmployeeAddress1;
command.Parameters.Add("@EmployeeAddress2", OleDbType.VarChar).Value = cm.EmployeeAddress2;
command.Parameters.Add("@EmployeeCity", OleDbType.VarChar).Value = cm.EmployeeCity;
command.Parameters.Add("@EmployeeState", OleDbType.VarChar).Value = cm.EmployeeState;
command.Parameters.Add("@EmployeeZip", OleDbType.VarChar).Value = cm.EmployeeZip;
command.Parameters.Add("@EmployeeHomePhone", OleDbType.VarChar).Value = cm.EmployeeHomePhone;
command.Parameters.Add("@EmployeeCellPhone", OleDbType.VarChar).Value = cm.EmployeeCellPhone;
command.Parameters.Add("@EmployeeHomeEmail", OleDbType.VarChar).Value = cm.EmployeeHomeEmail;
command.Parameters.Add("@EmergencyContactName", OleDbType.VarChar).Value = cm.EmployeeECName;
command.Parameters.Add("@EmergencyContactRelationship", OleDbType.VarChar).Value = cm.EmployeeECRelationship;
command.Parameters.Add("@EmergencyContactHomePhone", OleDbType.VarChar).Value = cm.EmployeeECHomePhone;
command.Parameters.Add("@EmergencyContactCellPhone", OleDbType.VarChar).Value = cm.EmployeeECCellPhone;
command.Parameters.Add("@EmergencyContactWorkPhone", OleDbType.VarChar).Value = cm.EmployeeECWorkPhone;
// Moved to the end and removed from the SET clause
command.Parameters.Add("@EmployeeLastName", OleDbType.VarChar).Value = cm.LastName;
command.Parameters.Add("@EmployeeFirstName", OleDbType.VarChar).Value = cm.FirstName;
try
{
var numberUpdated = command.ExecuteNonQuery();
MessageBox.Show("Record Updated = " + numberUpdated.ToString());
// connection.Close(); // not needed as the connection is closed and disposed by the using block its wrapped in
} catch (OleDbException ex)
{
Console.WriteLine(ex.Source);
MessageBox.Show(ex.Source);
}
}
}
'@EmployeeAddress1'
等参数周围使用此刻度标记时,我将其更改为此@EmployeeAddress1
(参数周围没有'
符号)CurrentTimeStamp
指定为varchar而不是某种类型的日期时间类型?答案 1 :(得分:0)
首先进行更改以获取更新的实际行数。
这将确认更新是否发生。
var rowCount = command.ExecuteNonQuery();
由于它没有更新,您使用的是自动提交关闭的数据库吗? SQL Server通常自动提交打开,Oracle关闭,在这种情况下,您需要使用事务并提交。