ADO.NET 中参数化 SQL 命令的最终形式

发布于 2024-08-31 00:48:27 字数 1051 浏览 1 评论 0原文

当我通过 ADO.NET 从 C# 程序向 Access 发送参数化查询时,出现语法错误。

当然,我知道我的代码中包含了什么 SQL 字符串,其中嵌入了参数名称。

有谁知道在我调用 cmd.ExecuteNonQuery 后如何查看最终发送到 DBMS 的 SQL 字符串?

谢谢。

编辑:

无法在交互式调试器或访问日志或其他内容中看到该字符串? 为了让任何人重现我的精确问题,他们必须拥有我的数据库,但这是不会发生的。然而,由于人们对我正在尝试做的事情的细节表示了兴趣,所以我发布了以下代码片段:

  OdbcCommand cmd = new OdbcCommand();
  cmd.CommandText = 
     @"insert into Posts (Page, Line, TimeStamp, Status) values
           (@pagename, @lineno, @now, 'SAVED')";
  cmd.Connection = _cn;
  cmd.Transaction = transaction;
  cmd.Parameters.Add(new OdbcParameter("@pagename",OdbcType.VarChar));
  cmd.Parameters.Add(new OdbcParameter("@lineno",OdbcType.VarChar));
  cmd.Parameters.Add(new OdbcParameter("@now",OdbcType.DateTime));
  cmd.Parameters["@pagename"].Value = pageId;
  cmd.Parameters["@lineno"].Value = lineId;
  cmd.Parameters["@now"].Value = now;
  cmd.ExecuteNonQuery();

我希望它有所帮助。

再次感谢。

编辑:

我发现“TimeStamp”可能是 AccessSQL 中的保留字,这可能是语法错误的原因。然而,即使假设这是原因,如何查看最终形式的 SQL 查询的一般问题仍然悬而未决。

I am getting a syntax error when I send a parameterized query to Access from my C# program via ADO.NET.

I, of course, know what SQL string I have included in my code, with the parameter names embedded inside.

Does anyone know how I can look at the SQL string that is finally sent to the DBMS during after I call cmd.ExecuteNonQuery?

Thanks.

EDIT:

There is no way to see that string in the interactive debugger or in an Access log or something?
In order for anyone to reproduce my precise problem, they would have to have my database, which isn't going to happen. However, since interest has been expressed in the details of what I'm trying to do I'm publishing the following code fragment:

  OdbcCommand cmd = new OdbcCommand();
  cmd.CommandText = 
     @"insert into Posts (Page, Line, TimeStamp, Status) values
           (@pagename, @lineno, @now, 'SAVED')";
  cmd.Connection = _cn;
  cmd.Transaction = transaction;
  cmd.Parameters.Add(new OdbcParameter("@pagename",OdbcType.VarChar));
  cmd.Parameters.Add(new OdbcParameter("@lineno",OdbcType.VarChar));
  cmd.Parameters.Add(new OdbcParameter("@now",OdbcType.DateTime));
  cmd.Parameters["@pagename"].Value = pageId;
  cmd.Parameters["@lineno"].Value = lineId;
  cmd.Parameters["@now"].Value = now;
  cmd.ExecuteNonQuery();

I hope it helps.

Thanks again.

EDIT:

It occurs to me that "TimeStamp" may be a reserved word in AccessSQL and this is likely the cause of the syntax error. However, even assuming this is the cause, the general question of how to see the SQL query in its final form remains open.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2024-09-07 00:48:27

Access 仅使用位置参数。

您所需要做的就是将 SQL 更改为:

insert into Posts (Page, Line, [TimeStamp], Status) values (?, ?, ?, 'SAVED')

确保以正确的顺序将任何其他参数放入 Parameters 集合中。

编辑:更新以解决保留字问题。

编辑: 不可能跟踪在 OLE DB 连接上运行的 SQL,至少目前不可能。请参阅这篇 Microsoft 知识库文章

Access only uses positional parameters.

All you need to do is change your SQL to this:

insert into Posts (Page, Line, [TimeStamp], Status) values (?, ?, ?, 'SAVED')

making sure to put any additional parameters into the Parameters collection in the proper order.

Edit: Updated to solve the reserved-word issue.

Edit: It is not possible to trace the SQL that gets run on an OLE DB connection, at least not currently. See this Microsoft KB article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文