Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 仅更新1条记录时返回值2的ExecuteOnQuery_C# 4.0_Enterprise Library_Executenonquery - Fatal编程技术网

C# 4.0 仅更新1条记录时返回值2的ExecuteOnQuery

C# 4.0 仅更新1条记录时返回值2的ExecuteOnQuery,c#-4.0,enterprise-library,executenonquery,C# 4.0,Enterprise Library,Executenonquery,运行EnterpriseLibrary5.0的示例,当我使用ExecuteOnQuery运行更新存储过程时,它返回2。更新基于ProductID,表的主键(是的,我选中了,它是唯一的) 以下是简单的存储过程: ALTER PROCEDURE [dbo].[UpdateProductsTable] @ProductID int, @ProductName varchar(50) = NULL, @CategoryID int = NULL, @Unit

运行EnterpriseLibrary5.0的示例,当我使用ExecuteOnQuery运行更新存储过程时,它返回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。单击messages选项卡显示

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
不知道为什么我会在这里看到这三次,但我不认为这是问题所在

以下是调用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.");
            }
        }
publicstaticvoid执行更新查询()
//更新单行的列,检查更新是否成功,然后再次更新以将其返回到原始值
{
字符串oldName=“Chai”;
字符串newName=“柴茶”;
SqlDatabase defaultDB=EnterpriseLibraryContainer.Current.GetInstance()作为SqlDatabase;
//Create命令执行存储过程并添加参数。
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,“单价”,DbType.Currency,18);
//执行查询并检查是否更新了一行。
int i=defaultDB.ExecuteNonQuery(cmd);
如果(i==1)
{
//更新成功。
}
其他的
{
WriteLine(“错误:无法仅更新一行。”);
}
//更改第二个参数的值
defaultDB.SetParameterValue(cmd,“ProductName”,oldName);
//执行查询并检查是否更新了一行
if(defaultDB.ExecuteNonQuery(cmd)==1)
{
//更新成功。
}
其他的
{
WriteLine(“错误:无法仅更新一行。”);
}
}

我使用inti查看方法的返回值,它返回2。你知道为什么会这样吗?这是VS2010中针对SQL 2005运行的EnterpriseLibarary5.0。非常简单但令人费解。

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

编辑:文档

发件人:

当插入或更新的表上存在触发器时,返回值包括受插入或更新操作影响的行数以及受触发器影响的行数


奥斯汀,我不知道!这正是它-一个更新触发器设置LastModified字段。现在根据值2进行验证-LOLIs是否存在,以避免受触发器影响的行,并仅获取受实际dml操作影响的行?@girishgupta211--我不这么认为,但这是一个向社区提问的好问题,我们可以从这里链接到它。@GirishGupta,我通过在SQL末尾添加一个
SELECT@@ROWCOUNT
语句并使用
ExecuteScalar
将其取回来解决这个问题。