【发布时间】:2022-01-16 16:27:48
【问题描述】:
使用C#代码删除SQL Server中的行时,返回的影响行数为2,但表中只有一项。这是代码。
int result = -1;
using (SqlConnection sqlConnection = new SqlConnection(AppConfiguration.ConnectionStringIguide))
{
string sql = string.Format("delete from atblOrders where OrderID='{0}'", orderId);
using (SqlCommand sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = sql;
sqlCommand.CommandType = CommandType.Text;
sqlConnection.Open();
result = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
我将 SQL 复制到 SQL Server Management Studio 并运行 SQL。它打印出两行受影响的 1 行。
(1 行受影响) (1 行受影响) 完成时间:2021-12-13T13:53:52.0466180+08:00
如果我使用具有相同 id 的 select 查询,它只会返回一项。那么,为什么删除时会影响两行?
【问题讨论】:
-
表上是否有删除另一个表中记录的DELETE触发器?
-
我同意它很可能是一个 DELETE 触发器,但这个触发器可能会插入、更新或删除。如果是出于审计跟踪目的,它很可能是插入!
-
@juergend。是的。我使用
select * from sysobjects a INNER JOIN sys.triggers b ON b.object_id = a.id INNER JOIN syscomments c ON c.id = a.id WHERE a.xtype = 'tr'检查触发器,我找到了一个与这张表相关的。
标签: c# sql-server