Petapoco - 使用多个输出参数调用存储过程

时间:2016-02-23 09:10:14

标签: c# petapoco

此代码可以正常工作:

var database = new Database("SpacesConnectionString");
var returnValue = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
var sqlScript = Sql.Builder.Append(";EXEC @0 " +
                   " @@ReturnValue = @1,",
    "[dbo].[USP_Test] "
    , returnValue
    );

database.EnableAutoSelect = false;
var result = database.Query<dynamic>(sqlScript).ToList();

如果底层存储过程未返回任何错误/输出参数。

不幸的是,我得到了这个例外:

  

对象引用未设置为对象的实例。

如果底层存储过程抛出异常。我跟踪生成的SQL,它工作正常。因此,与Petapoco相关的此代码必定存在问题。

PS:

这是一个更简单的版本:

IF EXISTS ( SELECT  *
            FROM    sys.objects
            WHERE   type = N'P'
                    AND name = N'USP_Test' )
    DROP PROCEDURE USP_Test
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: 
-- =============================================
CREATE PROCEDURE USP_Test
    @ReturnValue INT OUTPUT
AS
    BEGIN
        SET NOCOUNT ON;

        SELECT @ReturnValue = 1
    END 

代码:

var database = new Database("SpacesConnectionString");

var sql = new Sql().Append("DECLARE @@ReturnValue INT")
    .Append("EXECUTE [dbo].[USP_Test] @@ReturnValue OUTPUT")
    .Append("SELECT @@ReturnValue");

var ret = database.ExecuteScalar<int>(sql);

PPS:

看起来this link有帮助。这是上述简化示例的工作代码:

let ref = Firebase(url: "https://stackoverflow.firebaseio.com/35570687")
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
    if (!snapshot.exists()) {
        ref.setValue([".sv": "timestamp"])
    }
    else {
        print("already exist")
    }
})

1 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情:

namespace ConsoleApplication5
{
public class PetaPocoUnitOfWork : IUnitOfWork
{
    private readonly Transaction _petaTransaction;
    private readonly Database _db;

    public PetaPocoUnitOfWork(string connectionString)
    {
        _db = new Database(connectionString);
        _petaTransaction = new Transaction(_db);
    }

    public void Dispose()
    {
        _petaTransaction.Dispose();
    }

    public Database Db
    {
        get { return _db; }
    }

    public void Commit()
    {
        _petaTransaction.Complete();
    }
}

public interface IUnitOfWork : IDisposable
{
    void Commit();
    Database Db { get; }
}

public interface IUnitOfWorkProvider
{
    IUnitOfWork GetUnitOfWork(string connectionString);
}

public class PetaPocoUnitOfWorkProvider : IUnitOfWorkProvider
{
    public IUnitOfWork GetUnitOfWork(string connectionString)
    {
        return new PetaPocoUnitOfWork(connectionString);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var unitOfWorkProvider = new PetaPocoUnitOfWorkProvider();

        using (var uow = unitOfWorkProvider.GetUnitOfWork(""))
        {
            var errorNumber = new SqlParameter("@ErrorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
            var errorSeverity = new SqlParameter("@ErrorSeverity", SqlDbType.Int) { Direction = ParameterDirection.Output };
            var errorState = new SqlParameter("@ErrorState", SqlDbType.Int) { Direction = ParameterDirection.Output };
            var errorProcedure = new SqlParameter("@ErrorProcedure", SqlDbType.NVarChar) { Direction = ParameterDirection.Output, Size = 128 };
            var errorLine = new SqlParameter("@ErrorLine", SqlDbType.Int) { Direction = ParameterDirection.Output };
            var errorMessage = new SqlParameter("@ErrorMessage", SqlDbType.NVarChar) { Direction = ParameterDirection.Output, Size = 4000 };

            var sqlScript = Sql.Builder.Append(";EXEC @0 " +
                           " @@Uid = @1," +
                           " @@ErrorNumber = @2 OUTPUT," +
                           " @@ErrorSeverity = @3 OUTPUT," +
                           " @@ErrorState = @4 OUTPUT," +
                           " @@ErrorProcedure = @5 OUTPUT," +
                           " @@ErrorLine = @6 OUTPUT," +
                           " @@ErrorMessage = @7 OUTPUT",
            "[Schema].[USP_BlaDiBla]",
            new Guid("1E454A42-CC41-4FA1-BE91-1F7689986A23").ToString(),
            errorNumber,
            errorSeverity,
            errorState,
            errorProcedure,
            errorLine,
            errorMessage
           );

            var result = uow.Db.SingleOrDefault<object>(sqlScript);
        }
    }
}
}