在更新使用oledbcommand.executeNonQuery(MS访问记录的问题),导致无法更新 [英] Issue in updating MS Access records using oledbcommand.executeNonQuery(), result not updating

查看:338
本文介绍了在更新使用oledbcommand.executeNonQuery(MS访问记录的问题),导致无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴查询第一次来这里,所以,请忽略我的格式化。

I am posting a query first time here, So, Please ignore my formatting.

我试图使用update命令来更新我的.ACCDB文件,但结果 oledbcommand.executeNonQuery() 0 从而导致数据库没有更新。

I am trying to update my .accdb file using update command, but result of oledbcommand.executeNonQuery() is 0 hence result is not updating in the database.

虽然我收到没有错误。

下面是我在做什么。

string vsql = string.Format("UPDATE DefTask_List SET [Action]=@Action WHERE  [SNo]=@SNo");
vcom.Parameters.AddWithValue("@SNo", row.Cells[0].Value.ToString());
vcom.Parameters.AddWithValue("@Action", comboBox1.Text);

OleDbCommand vcom = new OleDbCommand(vsql, vcon);
vcon.Open();
int k = vcom.ExecuteNonQuery();
vcom.Dispose();
vcon.Close();

请注意, SNO 自动编号

推荐答案

的OleDbCommand 不支持命名的参数。唯一的事情就是他们的订单。

OleDbCommand doesn't support named parameters. The only matter is their orders.

从的 OleDbCommand.Parameters 属性

的OLE DB .NET提供程序不支持命名的参数传递
参数...

The OLE DB .NET Provider does not support named parameters for passing parameters...

因此,在顺序这OleDbParameter对象添加到
OleDbParameterCollection必须直接对应于
问号占位符的命令文本中参数的位置。

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

这就是为什么你的第一个 @Action 的OleDbCommand 匹配@SNo AddWithValue @SNo 与相匹配的 @Action AddWithValue

That's why your first @Action in OleDbCommand matches with @SNo in your AddWithValue and @SNo matches with your @Action in your AddWithValue.

由于可能是你没有数据这样,就不会有更新操作。

Since probably you don't have a data like this, there will be no update operation.

切换您的参数命令和使用的 。新增 方法是建议而不是 AddWithValue 。它可能会产生意想不到的结果。阅读;

Switch your parameter orders and use .Add method which is recommended instead of AddWithValue. It may generate unexpected results. Read;

  • Can we stop using AddWithValue() already?

还可以使用 使用语句处置你的的OleDbConnection 的OleDbCommand 而不是调用 .Dispose() .Close()手动的方法。

Also use using statement to dispose your OleDbConnection and OleDbCommand instead of calling .Dispose() and .Close() methods manually.

using(OleDbConnection vcon = new OleDbConnection(conString))
using(OleDbCommand vcom = vcon.CreateCommand())
{
    vcom.CommandText = "UPDATE DefTask_List SET [Action]=@Action WHERE [SNo]=@SNo";
    vcom.Parameters.Add("?", OleDbType.VarChar).Value = comboBox1.Text;
    vcom.Parameters.Add("?", OleDbType.Integer).Value = (int)row.Cells[0].Value;
    // I assume your column types are NVarchar2 and Int32
    vcon.Open();
    int k = vcom.ExecuteNonQuery();
}

这篇关于在更新使用oledbcommand.executeNonQuery(MS访问记录的问题),导致无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆