0

我正在学习 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
4

1 回答 1

4

要将输出参​​数用于接口实现,只需将该参数标记为输出参数:

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

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

于 2014-06-10T00:20:29.683 回答