文件获取“指定格式”无效.for ReturnValue参数值

本文关键字:for 无效 ReturnValue 参数 指定格式 获取 定格 格式 文件 | 更新日期: 2023-09-27 18:19:09

我试图使用SCOPE_IDENTITY返回一个长主键返回到c#使用的ReturnValue选项DynamicParameter

以下是来自Dapper网站的示例代码:
var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure); 
int b = p.Get<int>("@b");
int c = p.Get<int>("@c");

不返回int,我更愿意这样做,因为我的主键字段应该是bigint

var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int64, direction: ParameterDirection.ReturnValue);
cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure); 
int b = p.Get<int>("@b");
int c = p.Get<long>("@c");

在我的过程中,我使用"返回SCOPE_IDENTITY()"。

然而,这样做似乎会导致"指定的强制转换是无效的"异常。

文件获取“指定格式”无效.for ReturnValue参数值

存储过程的返回值总是隐式整数,即int。因此,您只能将视为整数。如果您试图将其作为long拆箱,将会失败。

选项:

  • 如果该值适合,只需将其视为。net侧的int
  • 否则使用bigint类型的out参数,并将其视为。net侧的long
  • 或使用selectQuery<long>(...).Single()

如果我没记错的话,SCOPE_IDENTITY()返回一个小数(http://msdn.microsoft.com/en-us/library/ms190315.aspx),因此会出现无效强制转换异常。

您需要将其强制转换为bigint(在SQL中)以使其按您希望的方式工作