我有一个在SQL Server 2005上运行的项目。我有一个存储过程接受一些输入参数,有2个输出参数,还从里面的select语句返回结果集。
数据不打算作为对象返回,只是一个没有主键的简单dto类'Foo'。
我尝试使用Dapper.Net和下面的代码,但是我得到了返回的异常错误
使用多映射API时,如果您拥有Id以外的密钥,请确保设置splitOn参数
代码:
var p = new DynamicParameters();
p.Add("@Code", code);
p.Add("@MessageControlId", getMessageControlId);
p.Add("@SuccessCode", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@ErrorDescription", dbType: DbType.String, direction: ParameterDirection.Output, size: 255);
var result = this.db.Query(sql: "up_MessageProcessed_get", param: p, commandType: CommandType.StoredProcedure);
我能用Dapper.Net做到这一点吗?我应该调用别的东西来获得简单的结果集吗?
根据要求,这是程序的shell, 它将设置返回值并返回1结果集。
CREATE PROCEDURE [dbo].[up_MessageProcessed_get]
@Code varchar(10),
@MessageControlID varchar(22),
@SuccessCode int OUTPUT,
@ErrorDescription varchar(255) OUTPUT
AS
BEGIN
Select 2 as MessageProcessID, 38349348 as StartNum, 3874900 as EndNum, GETDATE() as StartDate, GETDATE() as EndDate
SET @SuccessCode = 0
SET @ErrorDescription = 'Completed successfully'
END
答案 0 :(得分:1)
这不是“需要更多上下文”的“答案”,而是:在发布代码时,它工作正常(下图)。我怀疑这里的问题是特定于存储过程中的某些东西 - 它正在做的特殊事情:
public void TestIssue17648290()
{
var p = new DynamicParameters();
int code = 1, getMessageControlId = 2;
p.Add("@Code", code);
p.Add("@MessageControlId", getMessageControlId);
p.Add("@SuccessCode", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@ErrorDescription", dbType: DbType.String, direction: ParameterDirection.Output, size: 255);
connection.Execute(@"CREATE PROCEDURE #up_MessageProcessed_get
@Code varchar(10),
@MessageControlID varchar(22),
@SuccessCode int OUTPUT,
@ErrorDescription varchar(255) OUTPUT
AS
BEGIN
Select 2 as MessageProcessID, 38349348 as StartNum, 3874900 as EndNum, GETDATE() as StartDate, GETDATE() as EndDate
SET @SuccessCode = 0
SET @ErrorDescription = 'Completed successfully'
END");
var result = connection.Query(sql: "#up_MessageProcessed_get", param: p, commandType: CommandType.StoredProcedure);
var row = result.Single();
((int)row.MessageProcessID).IsEqualTo(2);
((int)row.StartNum).IsEqualTo(38349348);
((int)row.EndNum).IsEqualTo(3874900);
DateTime startDate = row.StartDate, endDate = row.EndDate;
p.Get<int>("SuccessCode").IsEqualTo(0);
p.Get<string>("ErrorDescription").IsEqualTo("Completed successfully");
}
输出:
Running TestIssue17648290 - OK!
(也就是说:工作正常)