通过 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。非常简单但令人困惑。