仅更新 1 条记录时,ExecuteNonQuery 返回值 2
运行 Enterprise Library 5.0 的示例,当我使用 ExecuteNonQuery 运行更新存储过程时,它返回 2。更新基于 ProductID,即表的主键(是的,我检查过,它是唯一的)。
这是简单的存储过程:
ALTER PROCEDURE [dbo].[UpdateProductsTable]
@ProductID int,
@ProductName varchar(50) = NULL,
@CategoryID int = NULL,
@UnitPrice money = NULL
AS
BEGIN
UPDATE Products
SET
ProductName = @ProductName,
CategoryID = @CategoryID,
UnitPrice = @UnitPrice
WHERE ProductID = @ProductID
END
在 SSMS 中执行此 sp 在查询结果窗口的右下角显示“1 行”,并在网格中返回值 0。单击消息选项卡显示
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
不确定为什么我在这里看到此消息 3 次,但我不认为这是问题所在。
下面是调用 sp 的代码:
public static void Exec_Update_Query()
//updates a column of a single row, checks that the update succeeded, and then update it again to return it to the original value
{
string oldName = "Chai";
string newName = "Chai Tea";
SqlDatabase defaultDB = EnterpriseLibraryContainer.Current.GetInstance<Database>() as SqlDatabase;
// Create command to execute the stored procedure and add the parameters.
DbCommand cmd = defaultDB.GetStoredProcCommand("UpdateProductsTable");
defaultDB.AddInParameter(cmd, "ProductID", DbType.Int32, 1);
defaultDB.AddInParameter(cmd, "ProductName", DbType.String, newName);
defaultDB.AddInParameter(cmd, "CategoryID", DbType.Int32, 1);
defaultDB.AddInParameter(cmd, "UnitPrice", DbType.Currency, 18);
// Execute the query and check if one row was updated.
int i = defaultDB.ExecuteNonQuery(cmd);
if (i == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
// Change the value of the second parameter
defaultDB.SetParameterValue(cmd, "ProductName", oldName);
// Execute query and check if one row was updated
if (defaultDB.ExecuteNonQuery(cmd) == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
}
我使用 int i 查看该方法的返回值,它返回 2。你知道为什么会这样吗?这是针对 SQL 2005 运行的 VS2010 中的 Enterprise Libarary 5.0。非常简单但令人困惑。
Running thru examples of Enterprise Library 5.0 and when I use ExecuteNonQuery to run an update sproc, it returns 2. The update is based on ProductID, the table's Primary Key (yes, I checked, and it is unique).
Here is the simple sproc:
ALTER PROCEDURE [dbo].[UpdateProductsTable]
@ProductID int,
@ProductName varchar(50) = NULL,
@CategoryID int = NULL,
@UnitPrice money = NULL
AS
BEGIN
UPDATE Products
SET
ProductName = @ProductName,
CategoryID = @CategoryID,
UnitPrice = @UnitPrice
WHERE ProductID = @ProductID
END
Executing this sp in SSMS shows "1 rows" in bottom right hand corner of the query results window, and a return value of 0 in the grid. Clicking on the messages tab shows
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
not sure why I'm seeing this 3 times here, but I don't believe that is the issue.
Here is the code calling the sp:
public static void Exec_Update_Query()
//updates a column of a single row, checks that the update succeeded, and then update it again to return it to the original value
{
string oldName = "Chai";
string newName = "Chai Tea";
SqlDatabase defaultDB = EnterpriseLibraryContainer.Current.GetInstance<Database>() as SqlDatabase;
// Create command to execute the stored procedure and add the parameters.
DbCommand cmd = defaultDB.GetStoredProcCommand("UpdateProductsTable");
defaultDB.AddInParameter(cmd, "ProductID", DbType.Int32, 1);
defaultDB.AddInParameter(cmd, "ProductName", DbType.String, newName);
defaultDB.AddInParameter(cmd, "CategoryID", DbType.Int32, 1);
defaultDB.AddInParameter(cmd, "UnitPrice", DbType.Currency, 18);
// Execute the query and check if one row was updated.
int i = defaultDB.ExecuteNonQuery(cmd);
if (i == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
// Change the value of the second parameter
defaultDB.SetParameterValue(cmd, "ProductName", oldName);
// Execute query and check if one row was updated
if (defaultDB.ExecuteNonQuery(cmd) == 1)
{
// Update succeeded.
}
else
{
Console.WriteLine("ERROR: Could not update just one row.");
}
}
I'm using int i to view the return value from the method and it returns 2. Any ideas why this would be? This is Enterprise Libarary 5.0 in VS2010 running against SQL 2005. Pretty straightforward but perplexing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话,由于您的命令而触发的任何触发器的结果也将包含在返回的行计数中。最有可能的是,这是你的问题。
编辑:文档
来自 MSDN SqlCommand.ExecuteNonQuery:
If I recall correctly, the result of any triggers that fire as a result of your commands will also be included in the returned row count. Most likely, this is your issue.
EDIT: Documentation
From MSDN SqlCommand.ExecuteNonQuery: