【问题标题】:C# EnterpriseLibrary trouble obtaining an Output ParameterC# EnterpriseLibrary 获取输出参数时遇到问题
【发布时间】:2021-01-25 15:04:49
【问题描述】:

我正在编写一个存储过程来捕获用户在 Web 应用程序中所做的某些注释。程序存储笔记,我需要输出参数来通知用户他/她的笔记是否被正确捕获。但是,我的输出参数有问题。

这是我的存储过程:

CREATE PROCEDURE captureNotes (
@IdFile bigint,
@IdSection int,
@Notes varchar(500),
@IsOk bit,
@User varchar(100),
@Bit bit OUTPUT)
AS
BEGIN
SET NOCOUNT ON
  BEGIN TRY
  --Checking if we are going to overwrite previous notes--
    DECLARE @Count int;
    SET @Count = (SELECT
      COUNT(IdFile)
    FROM WebValidation
    WHERE IdFile = @IdFile
    AND IdSection = @IdSection
    AND User = @User);
    IF @Count > 0
    BEGIN
      UPDATE WebValidation
      SET Notes = @Notes,
          IsOk = @IsOk, Date = GETDATE()
      WHERE IdFile = @IdFile
      AND IdSection = @IdSection
      AND User = @User
      SET @Bit=1;
    END
    ELSE
    BEGIN
      INSERT INTO WebValidation
        VALUES (@IdFile, @IdSection, @Notes, @IsOk, @User, GETDATE());
        SET @Bit=1;
    END
    SET @Bit = 1;
    SET NOCOUNT OFF
  END TRY
  BEGIN CATCH
    SET @Bit = 0;
    SET NOCOUNT OFF;
  END CATCH
END

我的 C# 代码

public int captureNotes(Int64 idFile, int idSection,
        string notes, bool isOk, string user)
    {
        try
        {
            db = DatabaseFactory.CreateDatabase("CnxPrincipal");
            cmd = db.GetStoredProcCommand("[captureNotes]");
            cmd.CommandTimeout = DBExecutionTimeout;

            db.AddInParameter(cmd, "@IdFile", DbType.Int64, idFile);
            db.AddInParameter(cmd, "@IdSection", DbType.Int16, idSection);
            db.AddInParameter(cmd, "@Notes", DbType.String, notes);
            db.AddInParameter(cmd, "@IsOk", DbType.Boolean, isOk);
            db.AddInParameter(cmd, "@User", DbType.String, user);
            int result = 0;
            db.AddOutParameter(cmd, "@Bit", DbType.Boolean, result);
            db.ExecuteNonQuery(cmd);
            return result;
        } catch (Exception ex) { throw ex; }
        finally { cmd.Dispose(); db = null; }
    }

代码将始终返回 0,尽管注释已成功添加。我究竟做错了什么? 感谢您的宝贵时间。

【问题讨论】:

    标签: c# sql asp.net .net enterprise-library


    【解决方案1】:

    AddOutParameter 不将值作为其最后一个参数,但它需要一个大小(对于布尔值,我想它只有一个字节)。此外,只有在您完成命令后,输出参数才包含有效值。因此,鉴于 AddOutParameter 是无效的,您需要一种方法来取回该参数并在执行查询后查看其值。

    我无法测试它,但遵循这条路径似乎是合乎逻辑的。

    db.AddOutParameter(cmd, "@Bit", DbType.Boolean, 1);
    db.ExecuteNonQuery(cmd);
    
    // Get the parameter back after the completition of the command.
    DbParameter par = cmd.Parameters.GetParameter("@Bit");
    
    // The value property is of type object and can be null, but given the SP
    // above it should never be null, so we can have
    return Convert.ToInt32(par.Value);
    

    【讨论】:

    • 感谢@Steve,您指出了正确的方向。但是,现在我想我发现了另一个问题。似乎我的 SP 没有返回任何东西,但是我找不到错误。如果我从 SQL Server 执行它,并尝试打印输出参数,它不会打印任何内容。
    • 从查询中可能是 INSERT 失败。例如,如果插入值中缺少某些字段,那么您应该得到一个异常。而那个 catch 块对理解问题没有任何帮助。我建议打印异常,然后尝试从 SSMS 运行它。
    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多