相关疑难解决方法(0)

如何使用Dapper执行插入并返回插入的标识?

如何对数据库执行插入并使用Dapper返回插入的标识?

我尝试过这样的事情:

string sql = "DECLARE @ID int; " +
             "INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff); " +
             "SELECT @ID = SCOPE_IDENTITY()";

var id = connection.Query<int>(sql, new { Stuff = mystuff}).First();
Run Code Online (Sandbox Code Playgroud)

但它没有用.

@Marc Gravell谢谢你的回复.我已经尝试过你的解决方案但是,下面仍有相同的异常跟踪

System.InvalidCastException: Specified cast is not valid

at Dapper.SqlMapper.<QueryInternal>d__a`1.MoveNext() in (snip)\Dapper\SqlMapper.cs:line 610
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in (snip)\Dapper\SqlMapper.cs:line 538
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param) in (snip)\Dapper\SqlMapper.cs:line 456
Run Code Online (Sandbox Code Playgroud)

c# sql-server dapper

161
推荐指数
4
解决办法
9万
查看次数

标签 统计

c# ×1

dapper ×1

sql-server ×1