如何在Insight.Database中使用输出参数

时间:2014-06-09 19:32:31

标签: insight.database

我正在学习Insight.Database,并希望将以下内容转换为使用存储库。

此版本有效:

using (SqlConnection connection = GetConnection())
        {
            string errorMessage = string.Empty;
            connection.Open();
            var selectCommand = new SqlCommand("cds_InsertUtilityIntroRate", connection);
            selectCommand.CommandType = CommandType.StoredProcedure;
            selectCommand.Parameters.Add(new SqlParameter() { ParameterName = "@utility_id", Value = introRateParameters.UtilityId });
            selectCommand.Parameters.Add(new SqlParameter() { ParameterName = "@start_date", Value = introRateParameters.StartDate });
            selectCommand.Parameters.Add(new SqlParameter() { ParameterName = "@intro_rate", Value = introRateParameters.IntroRate });
            selectCommand.Parameters.Add(new SqlParameter() { ParameterName = "@commodity_id", Value = introRateParameters.CommodityId });
            selectCommand.Parameters.Add(new SqlParameter() { ParameterName = "@zone", Value = introRateParameters.Zone });
            var output = new SqlParameter() {ParameterName = "@error", DbType = DbType.String, Size = 1000};
            output.Direction = ParameterDirection.Output;
            selectCommand.Parameters.Add(output);
            selectCommand.ExecuteNonQuery();

            if (output.Value is string)
            {
                errorMessage = output.Value.ToString();
            }

            return errorMessage;
        }

这是我为存储库整理的内容:

public interface  IIntroRateRespository
{
    int cds_InsertUtilityIntroRate(int utility_id, int commodity_id, DateTime start_date, decimal intro_rate, string zone, string error);
}

这是通过Insight.Database调用它的代码:

string errorMessage = string.Empty;
        var conn = GetConnection();
        var repository = conn.As<IIntroRateRespository>();
        int results = repository.cds_InsertUtilityIntroRate(introRateParameters.UtilityId, introRateParameters.CommodityId, introRateParameters.StartDate,
            introRateParameters.IntroRate, introRateParameters.Zone, errorMessage);

        Console.Write(errorMessage);

问题是有一个输出参数,我不确定应该如何连线。 当我运行它时,它会引发空引用异常:

    at Insight.Database.DBConnectionExtensions.<>c__DisplayClass10`1.<ExecuteScalar>b__f         (IDbCommand _, IDataReader __) in  c:\projects.net\Insight\Insight.Database\Insight.Database\Extensions\DBConnectionExtensions.cs:line 392
   at Insight.Database.DBConnectionExtensions.ExecuteAndAutoClose[T](IDbConnection connection, Func`2 getCommand, Func`3 translate, CommandBehavior commandBehavior) in c:\projects.net\Insight\Insight.Database\Insight.Database\Extensions\DBConnectionExtensions.cs:line 1488
   at Insight.Database.DBConnectionExtensions.ExecuteAndAutoClose[T](IDbConnection connection, Func`2 getCommand, Func`3 translate, Boolean closeConnection) in c:\projects.net\Insight\Insight.Database\Insight.Database\Extensions\DBConnectionExtensions.cs:line 1456
   at Insight.Database.DBConnectionExtensions.ExecuteScalar[T](IDbConnection connection, String sql, Object parameters, CommandType commandType, Boolean closeConnection, Nullable`1 commandTimeout, IDbTransaction transaction, Object outputParameters) in c:\projects.net\Insight\Insight.Database\Insight.Database\Extensions\DBConnectionExtensions.cs:line 386

1 个答案:

答案 0 :(得分:4)

要为接口实现使用输出参数,只需将参数标记为out参数:

interface IFoo {
     void MyProc(int inParameter, out outParameter, ref refParameter);
}

所以对你来说,它应该只是:

public interface IIntroRateRespository
{
    int cds_InsertUtilityIntroRate(int utility_id, int commodity_id, 
        DateTime start_date, decimal intro_rate, string zone, out string error);
}

另外,如果您的参数类看起来像这样:

class IntroRateParams {
    public int UtilityId;
    public DateTime StartDate;
    public decimal IntroRate;
    public int CommodityId;
    public string Zone;
    public string Error;
}

然后您可以将界面定义为:

public interface IIntroRateRespository
{
    int cds_InsertUtilityIntroRate(IntroRateParams parameters);
}

将单个对象传递给方法时,Insight将尝试从字段中提取参数。它还会自动将输出参数反射回对象。所以:

IntroRateParams p = /* from somewhere */
var repo = connection.As<IIntroRateRepository();
int id = repo.cds_InsertUtilityIntroRate(p);
var error = p.Error;

对于NullReferenceException:Insight正在生成NullReferenceException,因为您已将接口定义为返回 int 。 Insight正在调用ExecuteScalar并尝试将结果转换为int,但是您的过程没有返回任何行。

如果您的过程没有返回任何行,请确保您的方法返回void。

public interface IIntroRateRespository
{
    void cds_InsertUtilityIntroRate(int utility_id, int commodity_id, 
        DateTime start_date, decimal intro_rate, string zone, out string error);
}

我将更新库,以便在这种情况下异常更清楚。