使用 C# OleDbConnection 的 Microsoft Access UPDATE 命令和命令不起作用
不幸的是,由于更高的压力,我正在使用 Microsoft Access,并且尝试更新记录但没有成功。
这是代码:
private void UpdateContact(Contact contact)
{
using (OleDbConnection db = new OleDbConnection(_connString))
{
string query = "UPDATE [Contact] SET [FirstName] = @FirstName, [LastName] = @LastName, [MobileNumber] = @MobileNumber WHERE [Id] = @Id";
OleDbCommand cmd = new OleDbCommand(query, db) { CommandType = CommandType.Text };
cmd.Parameters.AddWithValue("@Id", contact.Id);
cmd.Parameters.AddWithValue("@FirstName", contact.FirstName);
cmd.Parameters.AddWithValue("@LastName", contact.LastName);
cmd.Parameters.AddWithValue("@MobileNumber", contact.MobileNumber);
db.Open();
int rowsAffected = cmd.ExecuteNonQuery();
db.Close();
}
}
一切似乎都很好,没有例外,但也没有 rowsAffected 。它总是返回 0。 我在调试时检查了这些值,并且应该保持正确。 使用 MS Access 2007 创建的访问文件,但其类型为 2002-2003。
知道我做错了什么吗?
I'm using Microsoft Access unfortunately because of higher forces and trying to update a record with no luck.
This is the code:
private void UpdateContact(Contact contact)
{
using (OleDbConnection db = new OleDbConnection(_connString))
{
string query = "UPDATE [Contact] SET [FirstName] = @FirstName, [LastName] = @LastName, [MobileNumber] = @MobileNumber WHERE [Id] = @Id";
OleDbCommand cmd = new OleDbCommand(query, db) { CommandType = CommandType.Text };
cmd.Parameters.AddWithValue("@Id", contact.Id);
cmd.Parameters.AddWithValue("@FirstName", contact.FirstName);
cmd.Parameters.AddWithValue("@LastName", contact.LastName);
cmd.Parameters.AddWithValue("@MobileNumber", contact.MobileNumber);
db.Open();
int rowsAffected = cmd.ExecuteNonQuery();
db.Close();
}
}
Everything seems to be fine, no exception but no rowsAffected either. It always returns 0.
I have checked the values while debugging and its the correct that should persist.
The access file created with MS Access 2007 but its type is of 2002-2003.
Any idea what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
按照语句的顺序添加您的参数,即名字...id
try
Add your parameters in the order of the statement, i.e. firstname...id
你需要吗?占位符和参数必须按出现的顺序添加: http ://msdn.microsoft.com/en-us/library/dw70f090.aspx
You need ? for the place holder and the parameters must be added in the order in which they occur: http://msdn.microsoft.com/en-us/library/dw70f090.aspx