仅更新 1 条记录时,ExecuteNonQuery 返回值 2

发布于 2024-12-05 16:05:58 字数 2470 浏览 0 评论 0原文

运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2024-12-12 16:05:58

如果我没记错的话,由于您的命令而触发的任何触发器的结果也将包含在返回的行计数中。最有可能的是,这是你的问题。

编辑:文档

来自 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:

When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文