有关生成的 (ADO.NET) DataAdapter (UpdateCommand) 中的 SQL 的问题

发布于 2024-11-02 14:31:47 字数 1923 浏览 2 评论 0原文

以下内容来自生成的数据适配器:

this._adapter.UpdateCommand.CommandText = @"
    UPDATE [dbo].[Currency2] SET [cid1] = @cid1, [cid2] = @cid2, [m] = @m 
    WHERE (([id] = @Original_id) 
    AND ([cid1] = @Original_cid1) 
    AND ([cid2] = @Original_cid2) 
    AND ([m] = @Original_m));

    SELECT id, cid1, cid2, m 
    FROM Currency2 
    WHERE (cid1 = @cid1) AND (cid2 = @cid2) AND (id = @id)";
  1. where 子句是否按照其正式定义实现乐观并发?

  2. 我最好的猜测是 SELECT 的目的是返回更新的行数,但是为什么它不使用 COUNT(*)?

更新:这是生成的更新代码,它给了我关于(2)中的 SELECT 的想法。

public virtual int Update(int cid1, int cid2, decimal m, int Original_id, int Original_cid1, int Original_cid2, decimal Original_m, int id) {
    this.Adapter.UpdateCommand.Parameters[0].Value = ((int)(cid1));
    this.Adapter.UpdateCommand.Parameters[1].Value = ((int)(cid2));
    this.Adapter.UpdateCommand.Parameters[2].Value = ((decimal)(m));
    this.Adapter.UpdateCommand.Parameters[3].Value = ((int)(Original_id));
    this.Adapter.UpdateCommand.Parameters[4].Value = ((int)(Original_cid1));
    this.Adapter.UpdateCommand.Parameters[5].Value = ((int)(Original_cid2));
    this.Adapter.UpdateCommand.Parameters[6].Value = ((decimal)(Original_m));
    this.Adapter.UpdateCommand.Parameters[7].Value = ((int)(id));
    global::System.Data.ConnectionState previousConnectionState = this.Adapter.UpdateCommand.Connection.State;
    if (((this.Adapter.UpdateCommand.Connection.State & global::System.Data.ConnectionState.Open) 
        != global::System.Data.ConnectionState.Open)) {
    this.Adapter.UpdateCommand.Connection.Open();
    }
    try {
    int returnValue = this.Adapter.UpdateCommand.ExecuteNonQuery();
    return returnValue;
    }
    finally {
    if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
        this.Adapter.UpdateCommand.Connection.Close();
    }
    }
}

This following is from a generated data adapter:

this._adapter.UpdateCommand.CommandText = @"
    UPDATE [dbo].[Currency2] SET [cid1] = @cid1, [cid2] = @cid2, [m] = @m 
    WHERE (([id] = @Original_id) 
    AND ([cid1] = @Original_cid1) 
    AND ([cid2] = @Original_cid2) 
    AND ([m] = @Original_m));

    SELECT id, cid1, cid2, m 
    FROM Currency2 
    WHERE (cid1 = @cid1) AND (cid2 = @cid2) AND (id = @id)";
  1. Is the where clause implementing optimistic concurrency per its formal definition?

  2. My best guess is that the purpose of the SELECT is to return number of updated rows, but then why doesn't it use COUNT(*)?

Update: Here is the generated UPDATE code, which gave me the ideas about the SELECT in (2).

public virtual int Update(int cid1, int cid2, decimal m, int Original_id, int Original_cid1, int Original_cid2, decimal Original_m, int id) {
    this.Adapter.UpdateCommand.Parameters[0].Value = ((int)(cid1));
    this.Adapter.UpdateCommand.Parameters[1].Value = ((int)(cid2));
    this.Adapter.UpdateCommand.Parameters[2].Value = ((decimal)(m));
    this.Adapter.UpdateCommand.Parameters[3].Value = ((int)(Original_id));
    this.Adapter.UpdateCommand.Parameters[4].Value = ((int)(Original_cid1));
    this.Adapter.UpdateCommand.Parameters[5].Value = ((int)(Original_cid2));
    this.Adapter.UpdateCommand.Parameters[6].Value = ((decimal)(Original_m));
    this.Adapter.UpdateCommand.Parameters[7].Value = ((int)(id));
    global::System.Data.ConnectionState previousConnectionState = this.Adapter.UpdateCommand.Connection.State;
    if (((this.Adapter.UpdateCommand.Connection.State & global::System.Data.ConnectionState.Open) 
        != global::System.Data.ConnectionState.Open)) {
    this.Adapter.UpdateCommand.Connection.Open();
    }
    try {
    int returnValue = this.Adapter.UpdateCommand.ExecuteNonQuery();
    return returnValue;
    }
    finally {
    if ((previousConnectionState == global::System.Data.ConnectionState.Closed)) {
        this.Adapter.UpdateCommand.Connection.Close();
    }
    }
}

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

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

发布评论

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

评论(2

追星践月 2024-11-09 14:31:47

该查询的作用是将更新的记录返回给应用程序以供将来使用,以便您更新内存中的对象,而不是更新之前的状态。它基本上是一个往返中的 UPDATE xxx WHERE ID=@IDSELECT xxx FROM xxx WHERE ID=@ID 命令。

What that query does is, returns the updated record back to the application for future use, so that you have updated object in memory, instead of it's state before update. It basically is UPDATE xxx WHERE ID=@ID and SELECT xxx FROM xxx WHERE ID=@ID commands in one roundtrip.

最好是你 2024-11-09 14:31:47

where 子句是否实现
乐观并发按照其形式
定义?

有点像。它会检查每一列,看看是否有人更改了其中的任何列。如果记录已更改,则不会更新。

然而,一个定义(来自维基百科)表示,如果记录发生更改,则事务将回滚。这并没有发生在这里。

Is the where clause implementing
optimistic concurrency per its formal
definition?

Sort of. It's checking every single column to see if anyone has changed any of them. If the record HAS changed, it's not updated.

However, one definition (from Wikipedia) says the transaction is rolled back if the record has changed. That isn't happening here.

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