【问题标题】:Auto generated column is not refreshed in the DataRow自动生成的列未在 DataRow 中刷新
【发布时间】:2015-11-10 02:24:46
【问题描述】:
其中一列名为 ID 我的 SQL Server 数据库是自动生成的。当我使用SqlDataAdapter.Update(table) 插入新行并接受更改SqlDataAdapter.AcceptChanges() 时,table 中的ID 列设置为-1,而不是新自动生成的ID 值。数据库插入工作和新行以顺序自动生成的IDvalues 插入数据库。
如何强制 SqlDataAdapter 或 SqlDataTable 取回正确的 ID 值?
【问题讨论】:
标签:
c#
sql-server
ado.net
【解决方案1】:
我已经解决了这个问题。供以后参考
operationDBAdapter.InsertCommand.CommandText = "INSERT INTO Operations(operationType,agentID,resetTime,description,enabled,logLevel) VALUES
(@operationType,@agentID,@resetTime,@description,@enabled,@logLevel);
**SELECT ID,operationType,agentID,resetTime,description,enabled,logLevel
FROM Operations WHERE (ID = SCOPE_IDENTITY())**"
插入命令后,只需从同一个表中选择行。