码迷,mamicode.com
首页 > 移动开发 > 详细

dapper的一个小扩展以支持dataset

时间:2015-07-08 14:12:22      阅读:2446      评论:0      收藏:0      [点我收藏+]

标签:

废话不多,直接上方法

 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‘ });

dapper的一个小扩展以支持dataset

标签:

原文地址:http://www.cnblogs.com/wujf/p/dapper.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!