我有以下SQL存储过程
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetNextNumberTest]
@NEWNUMBER varchar(100) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET @NEWNUMBER = 'ABC00001'
RETURN 1
END
我正在尝试使用dapper读取NENUMBER值,这是我的代码
private string GetNextNumber()
{
var dynamicParams = new DynamicParameters();
dynamicParams.Add("rval", DbType.Int32, direction: ParameterDirection.ReturnValue);
dynamicParams.Add("NEWNUMBER", DbType.String, direction: ParameterDirection.Output);
var data = db.QueryMultiple("GetNextNumberTest", dynamicParams, commandType: CommandType.StoredProcedure);
var result = dynamicParams.Get<string>("NEWNUMBER");
return result;
}
但我总是得到以下错误
An exception of type 'System.Data.SqlClient.SqlException' occurred in Dapper.dll but was not handled in user code
Additional information: Error converting data type varchar to int.
非常感谢任何帮助。
答案 0 :(得分:1)
多次使用错误:
DbType.Int32
/ DbType.String
作为值而不是数据类型 Execute
,而不是QueryMultiple
此处Execute
的返回来表示存储过程的返回值(这根本不是ADO.NET的工作原理;理论上返回值受影响的行,但它通常是无意义的数字)所以:
dynamicParams.Add("rval", dbType: DbType.Int32,
direction: ParameterDirection.ReturnValue);
dynamicParams.Add("NEWNUMBER", dbType: DbType.String,
direction: ParameterDirection.Output, size: 100);
db.Execute("GetNextNumberTest", dynamicParams,
commandType: CommandType.StoredProcedure);
var rval = dynamicParams.Get<int>("rval");
var result = dynamicParams.Get<string>("NEWNUMBER");