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