C# EnterpriseLibrary 无法获取输出参数

C# EnterpriseLibrary trouble obtaining an Output Parameter

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

这是我的存储过程:

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; }
    }

尽管注释添加成功,代码总是return0。我究竟做错了什么? 谢谢你的时间。

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);