数据库命令对象的参数顺序真的很重要吗?

发布于 2024-12-01 04:32:45 字数 1446 浏览 5 评论 0原文

我正在调试数据库操作代码,发现正确的更新从未发生,尽管代码从未失败。这是代码:

        condb.Open();
        OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=?,sentence=?,mp3=? WHERE id=? AND exercise_id=?", condb);
        dbcom.Parameters.AddWithValue("id", wd.ID);
        dbcom.Parameters.AddWithValue("exercise_id", wd.ExID);
        dbcom.Parameters.AddWithValue("word", wd.Name);
        dbcom.Parameters.AddWithValue("sentence", wd.Sentence);
        dbcom.Parameters.AddWithValue("mp3", wd.Mp3);

但是经过一些调整后,它起作用了:

        condb.Open();
        OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=?,sentence=?,mp3=? WHERE id=? AND exercise_id=?", condb);
        dbcom.Parameters.AddWithValue("word", wd.Name);
        dbcom.Parameters.AddWithValue("sentence", wd.Sentence);
        dbcom.Parameters.AddWithValue("mp3", wd.Mp3);                         
        dbcom.Parameters.AddWithValue("id", wd.ID);
        dbcom.Parameters.AddWithValue("exercise_id", wd.ExID);
  1. 为什么在 OleDb 连接的情况下必须在 WHERE 子句中的参数最后给出这一点如此重要?以前使用过 MySQL,我可以(并且通常这样做)首先写入 WHERE 子句的参数,因为这对我来说更符合逻辑。

  2. 一般查询数据库时参数顺序重要吗?一些性能问题或其他问题?

  3. 对于 DB2、Sqlite 等其他数据库,是否有要维护的特定顺序?

更新:我去掉了?,并包含了带有和不带有@的专有名称。顺序真的很重要。在这两种情况下,仅当最后提到 WHERE 子句参数时,才会发生实际更新。更糟糕的是,在复杂的查询中,我们很难知道 Access 期望哪个顺序,并且在顺序更改的所有情况下,查询都不会在没有警告/错误的情况下执行其预期职责!

I was debugging a database operation code and I found that proper UPDATE was never happening though the code never failed as such. This is the code:

        condb.Open();
        OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=?,sentence=?,mp3=? WHERE id=? AND exercise_id=?", condb);
        dbcom.Parameters.AddWithValue("id", wd.ID);
        dbcom.Parameters.AddWithValue("exercise_id", wd.ExID);
        dbcom.Parameters.AddWithValue("word", wd.Name);
        dbcom.Parameters.AddWithValue("sentence", wd.Sentence);
        dbcom.Parameters.AddWithValue("mp3", wd.Mp3);

But after some tweaking this worked:

        condb.Open();
        OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=?,sentence=?,mp3=? WHERE id=? AND exercise_id=?", condb);
        dbcom.Parameters.AddWithValue("word", wd.Name);
        dbcom.Parameters.AddWithValue("sentence", wd.Sentence);
        dbcom.Parameters.AddWithValue("mp3", wd.Mp3);                         
        dbcom.Parameters.AddWithValue("id", wd.ID);
        dbcom.Parameters.AddWithValue("exercise_id", wd.ExID);
  1. Why is it so important that the parameters in WHERE clause has to be given the last in case of OleDb connection? Having worked with MySQL previously, I could (and usually do) write parameters of WHERE clause first because that's more logical to me.

  2. Is parameter order important when querying database in general? Some performance concern or something?

  3. Is there a specific order to be maintained in case of other databases like DB2, Sqlite etc?

Update: I got rid of ? and included proper names with and without @. The order is really important. In both cases only when WHERE clause parameters was mentioned last, actual update happened. To make matter worse, in complex queries, its hard to know ourselves which order is Access expecting, and in all situations where order is changed, the query doesnt do its intended duty with no warning/error!!

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

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

发布评论

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

评论(3

戒ㄋ 2024-12-08 04:32:45

在 Access 中,ADODB.Command 对象会忽略参数名称。事实上,我可以使用虚假名称(该名称甚至不存在于 SQL 语句中)来引用参数,而 ADO 并不关心。它似乎只关心您提供参数值的顺序与这些参数在 SQL 语句中出现的顺序完全相同。顺便说一句,如果我使用 ? 占位符而不是命名参数构建 SQL 语句,也会发生这种情况。

虽然我意识到您的问题是关于 c# 和 OleDbCommand,但在我看来,Dot.Net 的 OleDbCommand 可能与 Access 的 ADODB.Command< 操作相同/代码>。不幸的是,我不了解 Dot.Net……但这是我的预感。 :-)

Within Access, an ADODB.Command object ignores parameter names. In fact I can refer to a parameter using a bogus name (which doesn't even exist in the SQL statement) and ADO doesn't care. All it seems to care about is that you supply parameter values in the exact same order as those parameters appear in the SQL statement. BTW, that is also what happens if I build the SQL statement with ? place-holders instead of named parameters.

While I realize that your question is about c# and OleDbCommand, it looks to me like Dot.Net's OleDbCommand may be operating the same as Access' ADODB.Command. Unfortunately, I don't know Dot.Net ... but that is my hunch. :-)

情栀口红 2024-12-08 04:32:45

由于使用了 ?,所以顺序很重要。命令字符串中的占位符。

如果要以任意顺序列出参数,最好使用命名参数,例如@word、@sentence等。

condb.Open();
OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=@word,sentence=@sentence,mp3=@mp3 WHERE id=@id AND exercise_id=@exercise_id", condb);
dbcom.Parameters.AddWithValue("@id", wd.ID);
dbcom.Parameters.AddWithValue("@exercise_id", wd.ExID);
dbcom.Parameters.AddWithValue("@word", wd.Name);
dbcom.Parameters.AddWithValue("@sentence", wd.Sentence);
dbcom.Parameters.AddWithValue("@mp3", wd.Mp3);                         

The order is important because of the use of ? placeholders in the command string.

If you want to list the parameters in any order, it's best to use named parameters, such as @word, @sentence, etc.

condb.Open();
OleDbCommand dbcom = new OleDbCommand("UPDATE Word SET word=@word,sentence=@sentence,mp3=@mp3 WHERE id=@id AND exercise_id=@exercise_id", condb);
dbcom.Parameters.AddWithValue("@id", wd.ID);
dbcom.Parameters.AddWithValue("@exercise_id", wd.ExID);
dbcom.Parameters.AddWithValue("@word", wd.Name);
dbcom.Parameters.AddWithValue("@sentence", wd.Sentence);
dbcom.Parameters.AddWithValue("@mp3", wd.Mp3);                         
吃不饱 2024-12-08 04:32:45

我一直在使用 OleDbCommand 及其参数集合针对 Access DB 进行一些测试。参数的排序当然是必要的,因为这是 OLE DB .NET 提供程序的限制。但是使用问号作为占位符时可能会遇到一个问题。

假设您的 Access DB 中有一个查询(“存储过程”),如下所示,这里非常简化:

parameters
  prmFirstNumber Long,
  prmSecondNumber Long;
select
  fullName
from
  tblPersons
where 
  numberOfCars < prmFirstNumber And
  numberOfPets < prmSecondNumber And
  numberOfBooks beteween prmFirstNumber And prmSecondNumber

在这里您可以看到,简单地更改为问号就会破坏查询。

但我发现,作为解决此问题的方法,您实际上可以使用参数名称。所以你可以让上面的查询保持原样。您只需在运行查询时使用相同的顺序即可。就像在本例中一样,您首先添加参数 prmFirstNumber,然后添加 prmSecondNumber,然后运行查询。

当重用参数时,即多次执行查询并每次为参数设置新值时,必须在定义参数后立即调用命令对象的准备方法。还有一些细节需要满足,请参阅“准备”文档。不调用准备会导致奇怪的行为,并且没有错误消息,这可能会损坏数据库或导致向用户提供错误的信息。

我还可以补充一点,当查询存储在指定参数的 Access DB 中时(如上面的示例),参数的顺序由参数部分明确定义。

我还创建了一个例程“retrieveDeclaredJetParametersInOrder”,它会按照正确的顺序自动使用这些命名参数填充 OleDbCommand 对象。所以我的代码可以如下所示:

Dim cmd As New OleDbCommand("qryInAccessDB", Conn)
cmd.CommandType = CommandType.StoredProcedure
Conn.Open()
retrieveDeclaredJetParametersInOrder(cmd)
cmd.Parameters("prmOneOfTheParametersPerhapsTheLastOneDeclared").Value = 1
cmd.Parameters("prmAnotherone").Value = 20
cmd.Parameters("prmYetAnotherPerhapsTheFirstOneDeclared").Value = 300
cmd.ExecuteNonQuery()
Conn.Close()

所以,正如您所看到的,我可以像参数命名一样处理它,而不必担心它们的顺序。

retrieveDeclaredJetParametersInOrder 当然会增加额外的执行时间,因为它涉及对数据库的额外调用,在数据库中检索 SQL 文本,然后解析出参数名称和类型。

I have been doing some tests with using OleDbCommand and its parameters collection against an Access DB. The ordering of parameters is of course necessary, since this is a limitation of the OLE DB .NET provider. But there is a problem that you can encounter when using question marks as place holders.

Say you have a query ("stored procedure") in your Access DB that looks like this, very simplified here:

parameters
  prmFirstNumber Long,
  prmSecondNumber Long;
select
  fullName
from
  tblPersons
where 
  numberOfCars < prmFirstNumber And
  numberOfPets < prmSecondNumber And
  numberOfBooks beteween prmFirstNumber And prmSecondNumber

Here you see that simply changing to question marks would break the query.

I have found though, as a solution to this, that you can actually use names for parameters. So you can let the query above remain as it is. You just have to use the same order when you run the query. Like in this case, you first add the parameter prmFirstNumber and then prmSecondNumber, and then you run the query.

When reusing parameters, i.e. executing a query more than once and setting new values for the parameters each time, one must call the prepare method of the command object right after having defined the parameters. There are some details there that need to be fulfilled too, look at the documentation for "prepare". Not calling prepare causes strange behaviour without error messages which can corrupt your database or cause wrong information to be presented to users.

I can add also that when queries are stored in the Access DB with parameters specified, like in my example above, then the ordering of the parameters is unambiguously defined by the parameters-section.

I also made a routine, "retrieveDeclaredJetParametersInOrder", which automatically populates an OleDbCommand object with those named parameters, in the correct order. So my code can look like this:

Dim cmd As New OleDbCommand("qryInAccessDB", Conn)
cmd.CommandType = CommandType.StoredProcedure
Conn.Open()
retrieveDeclaredJetParametersInOrder(cmd)
cmd.Parameters("prmOneOfTheParametersPerhapsTheLastOneDeclared").Value = 1
cmd.Parameters("prmAnotherone").Value = 20
cmd.Parameters("prmYetAnotherPerhapsTheFirstOneDeclared").Value = 300
cmd.ExecuteNonQuery()
Conn.Close()

So, as you see, I can handle it as if parameters are named, and never have to bother with their ordering.

The retrieveDeclaredJetParametersInOrder of course adds extra time to execution, since it involves an extra call to the DB, where it retrieves the SQL-text and then parses out the parameter names and types.

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