有关生成的 (ADO.NET) DataAdapter (UpdateCommand) 中的 SQL 的问题
以下内容来自生成的数据适配器:
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)";
where 子句是否按照其正式定义实现乐观并发?
我最好的猜测是 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)";
Is the where clause implementing optimistic concurrency per its formal definition?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该查询的作用是将更新的记录返回给应用程序以供将来使用,以便您更新内存中的对象,而不是更新之前的状态。它基本上是一个往返中的
UPDATE xxx WHERE ID=@ID
和SELECT 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
andSELECT xxx FROM xxx WHERE ID=@ID
commands in one roundtrip.有点像。它会检查每一列,看看是否有人更改了其中的任何列。如果记录已更改,则不会更新。
然而,一个定义(来自维基百科)表示,如果记录发生更改,则事务将回滚。这并没有发生在这里。
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.