dapper的一个小扩展以支持dataset

本文介绍了一种使用 Dapper 实现 ExecuteDataSet 的方法,通过该方法可以方便地从数据库中获取 DataSet 并填充到内存中。示例代码展示了如何为 MySql 数据库执行 SQL 查询并获取所有角色关系。

废话不多,直接上方法

 1         public static DataSet ExecuteDataSet(this IDbConnection cnn, IDbDataAdapter adapter, string sql, object param = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
 2         {
 3             var ds = new DataSet();
 4             var command = new CommandDefinition(sql, (object)param, null, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None);
 5             var identity = new Identity(command.CommandText, command.CommandType, cnn, null, param == null ? null : param.GetType(), null);
 6             var info = GetCacheInfo(identity, param, command.AddToCache);
 7             bool wasClosed = cnn.State == ConnectionState.Closed;
 8             if (wasClosed) cnn.Open();
 9             adapter.SelectCommand = command.SetupCommand(cnn, info.ParamReader);
10             adapter.Fill(ds);
11             if (wasClosed) cnn.Close();
12             return ds;
13         }

在Query方法下面添加即可

使用Demo

var mysqlDs = con.ExecuteDataSet(new MySqlDataAdapter(), "SELECT * FROM wjf_role_relation where ROLE_ID=@ROLE_ID", new { ROLE_ID = '1' });

转载于:https://www.cnblogs.com/wujf/p/dapper.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值