using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks; namespace DAL
{
/// <summary>
///DapperHelper
/// </summary>
public class DapperMySQLHelp
{ private string connection = ""; public DapperMySQLHelp() { } public DapperMySQLHelp(string connStr)
{
connection = connStr;
}
public IDbConnection Connection()
{
var conn = new MySqlConnection(connection);
conn.Open();
return conn;
} #region +ExcuteNonQuery 增、删、改同步操作
/// <summary>
/// 增、删、改同步操作
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">链接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>int</returns>
public int ExcuteNonQuery<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
int result = ;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
result = con.Execute(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
result = con.Execute(cmd, param, null, null, CommandType.Text);
}
}
return result;
}
#endregion #region +ExcuteNonQueryAsync 增、删、改异步操作
/// <summary>
/// 增、删、改异步操作
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">链接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>int</returns>
public async Task<int> ExcuteNonQueryAsync<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
int result = ;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
result = await con.ExecuteAsync(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
result = await con.ExecuteAsync(cmd, param, null, null, CommandType.Text);
}
}
return result;
}
#endregion #region +ExecuteScalar 同步查询操作
/// <summary>
/// 同步查询操作
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>object</returns>
public object ExecuteScalar(string cmd, DynamicParameters param = null, bool flag = true)
{
object result = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
result = con.ExecuteScalar(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
result = con.ExecuteScalar(cmd, param, null, null, CommandType.Text);
}
}
return result;
}
#endregion #region +ExecuteScalarAsync 异步查询操作
/// <summary>
/// 异步查询操作
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>object</returns>
public async Task<object> ExecuteScalarAsync(string cmd, DynamicParameters param = null, bool flag = true)
{
object result = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
result = await con.ExecuteScalarAsync(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
result = con.ExecuteScalarAsync(cmd, param, null, null, CommandType.Text);
}
}
return result;
}
#endregion #region +FindOne 同步查询一条数据
/// <summary>
/// 同步查询一条数据
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>t</returns>
public T FindOne<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
IDataReader dataReader = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
}
if (dataReader == null || !dataReader.Read()) return null;
Type type = typeof(T);
T t = new T();
foreach (var item in type.GetProperties())
{
for (int i = ; i < dataReader.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
var kvalue = dataReader[item.Name];
if (kvalue == DBNull.Value) continue;
item.SetValue(t, kvalue, null);
break;
}
}
return t;
}
}
#endregion #region +FindOne 异步查询一条数据
/// <summary>
/// 异步查询一条数据
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>t</returns>
public async Task<T> FindOneAsync<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
IDataReader dataReader = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.Text);
}
if (dataReader == null || !dataReader.Read()) return null;
Type type = typeof(T);
T t = new T();
foreach (var item in type.GetProperties())
{
for (int i = ; i < dataReader.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
var kvalue = dataReader[item.Name];
if (kvalue == DBNull.Value) continue;
item.SetValue(t, kvalue, null);
break;
}
}
return t;
}
}
#endregion #region +FindToList 同步查询数据集合
/// <summary>
/// 同步查询数据集合
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>t</returns>
public IList<T> FindToList<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
IDataReader dataReader = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
}
if (dataReader == null || !dataReader.Read()) return null;
Type type = typeof(T);
List<T> tlist = new List<T>();
while (dataReader.Read())
{
T t = new T();
foreach (var item in type.GetProperties())
{
for (int i = ; i < dataReader.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
var kvalue = dataReader[item.Name];
if (kvalue == DBNull.Value) continue;
item.SetValue(t, kvalue, null);
break;
}
}
if (tlist != null) tlist.Add(t);
}
return tlist;
}
}
#endregion #region +FindToListAsync 异步查询数据集合
/// <summary>
/// 异步查询数据集合
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>t</returns>
public async Task<IList<T>> FindToListAsync<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
IDataReader dataReader = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.Text);
}
if (dataReader == null || !dataReader.Read()) return null;
Type type = typeof(T);
List<T> tlist = new List<T>();
while (dataReader.Read())
{
T t = new T();
foreach (var item in type.GetProperties())
{
for (int i = ; i < dataReader.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
var kvalue = dataReader[item.Name];
if (kvalue == DBNull.Value) continue;
item.SetValue(t, kvalue, null);
break;
}
}
if (tlist != null) tlist.Add(t);
}
return tlist;
}
}
#endregion #region +FindToList 同步查询数据集合
/// <summary>
/// 同步查询数据集合
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>t</returns>
public IList<T> FindToListAsPage<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
IDataReader dataReader = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
}
if (dataReader == null || !dataReader.Read()) return null;
Type type = typeof(T);
List<T> tlist = new List<T>();
while (dataReader.Read())
{
T t = new T();
foreach (var item in type.GetProperties())
{
for (int i = ; i < dataReader.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
var kvalue = dataReader[item.Name];
if (kvalue == DBNull.Value) continue;
item.SetValue(t, kvalue, null);
break;
}
}
if (tlist != null) tlist.Add(t);
}
return tlist;
}
}
#endregion #region +FindToListByPage 同步分页查询数据集合
/// <summary>
/// 同步分页查询数据集合
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>t</returns>
public IList<T> FindToListByPage<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
IDataReader dataReader = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
}
if (dataReader == null || !dataReader.Read()) return null;
Type type = typeof(T);
List<T> tlist = new List<T>();
while (dataReader.Read())
{
T t = new T();
foreach (var item in type.GetProperties())
{
for (int i = ; i < dataReader.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
var kvalue = dataReader[item.Name];
if (kvalue == DBNull.Value) continue;
item.SetValue(t, kvalue, null);
break;
}
}
if (tlist != null) tlist.Add(t);
}
return tlist;
}
}
#endregion #region +FindToListByPageAsync 异步分页查询数据集合
/// <summary>
/// 异步分页查询数据集合
/// 2016-10-26
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="connection">连接字符串</param>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>t</returns>
public async Task<IList<T>> FindToListByPageAsync<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new()
{
IDataReader dataReader = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
dataReader = await con.ExecuteReaderAsync(cmd, param, null, null, CommandType.Text);
}
if (dataReader == null || !dataReader.Read()) return null;
Type type = typeof(T);
List<T> tlist = new List<T>();
while (dataReader.Read())
{
T t = new T();
foreach (var item in type.GetProperties())
{
for (int i = ; i < dataReader.FieldCount; i++)
{
//属性名与查询出来的列名比较
if (item.Name.ToLower() != dataReader.GetName(i).ToLower()) continue;
var kvalue = dataReader[item.Name];
if (kvalue == DBNull.Value) continue;
item.SetValue(t, kvalue, null);
break;
}
}
if (tlist != null) tlist.Add(t);
}
return tlist;
}
}
#endregion #region +QueryPage 同步分页查询操作
/// <summary>
/// 同步分页查询操作
/// </summary>
/// <param name="sql">查询语句</param>
/// <param name="orderBy">排序字段</param>
/// <param name="pageIndex">当前页码</param>
/// <param name="pageSize">页面容量</param>
/// <param name="count">总条数</param>
/// <param name="param">参数</param>
/// <param name="strWhere">条件</param>
/// <returns>返回结果的数据集合</returns>
public List<Dictionary<string, Object>> QueryPage(string sql, string orderBy, int pageIndex, int pageSize, out int count, object param = null, string strWhere = "")
{
count = ;
List<Dictionary<String, Object>> list = new List<Dictionary<string, object>>(); if (sql.Contains("where"))
{
sql = sql + strWhere;
}
else
{
sql = sql + " where 1=1 " + strWhere;
} string strSQL = "SELECT (@i:=@i+1) AS row_id,tab.* FROM (" + sql + ") AS TAB,(SELECT @i:=0) AS it ORDER BY " + orderBy + " LIMIT " + (pageIndex - ) + "," + pageSize; list = QueryData(strSQL, param, false); string strCount = "SELECT count(*) FROM (" + sql + ") tcount";
count = Convert.ToInt32(ExecuteScalar(strCount)); return list;
}
#endregion #region +QueryData 同步查询数据集合
/// <summary>
/// 同步查询数据集合
/// </summary>
/// <param name="cmd">sql语句</param>
/// <param name="param">参数</param>
/// <param name="flag">true存储过程,false sql语句</param>
/// <returns>t</returns>
public List<Dictionary<String, object>> QueryData(string cmd, object param = null, bool flag = false)
{
IDataReader dataReader = null;
using (MySqlConnection con = new MySqlConnection(connection))
{
if (flag)
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.StoredProcedure);
}
else
{
dataReader = con.ExecuteReader(cmd, param, null, null, CommandType.Text);
}
List<Dictionary<String, object>> list = new List<Dictionary<string, object>>();
Dictionary<String, object> dic = null;
string colName = "";
while (dataReader.Read())
{
dic = new Dictionary<string, object>(); for (int i = ; i < dataReader.FieldCount; i++)
{
colName = dataReader.GetName(i);
dic.Add(colName, dataReader[colName]);
} if (dic.Keys.Count > )
{
list.Add(dic);
}
}
return list;
}
}
#endregion }
}

Dapper扩展的更多相关文章

  1. Asp.net 面向接口可扩展框架之数据处理模块及EntityFramework扩展和Dapper扩展(含干货)

    接口数据处理模块是什么意思呢?实际上很简单,就是使用面向接口的思想和方式来做数据处理. 还提到EntityFramework和Dapper,EntityFramework和Dapper是.net环境下 ...

  2. dapper 扩展插件: Rainbow

    dapper 扩展插件: Rainbow dapper 是一个效率非常高的orm  框架 ,效率要远远大于 我们大微软的EF .    它只有一个类文件,非常之小. 1,首先下载dapper  这里下 ...

  3. NET Core 实战 Dapper 扩展数据访问

    NET Core 实战:基于 Dapper 扩展你的数据访问方法 一.前言 在非静态页面的项目开发中,必定会涉及到对于数据库的访问,最开始呢,我们使用 Ado.Net,通过编写 SQL 帮助类帮我们实 ...

  4. .NET MVC5+ Dapper+扩展+AutoFac自动注入实现

    1.首先创建一个MVC项目 定义Model 层  view 层 index.cshtml  控制器层Controllers等文件 2.在线安装或者引用dapper 以及扩展相关包 同时Autofac ...

  5. .NET MVC5+ Dapper+扩展+微软Unity依赖注入实例

    1.dapper和dapper扩展需要在线安装或者引用DLL即可 使用nuget为项目增加Unity相关的包 2.model类 public class UserInfo { public int I ...

  6. Dapper扩展Dapper.Common框架 Linq To Sql 底层源码.net ORM框架

    源代码:https://github.com/1448376744/Dapper.CommonNUGET: Dapper.CommonQQ群:642555086 一.基本结构,此处可用委托,或动态代理 ...

  7. Dapper扩展之~~~Dapper.Contrib

    平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...

  8. ASP.NET Core 实战:基于 Dapper 扩展你的数据访问方法

    一.前言 在非静态页面的项目开发中,必定会涉及到对于数据库的访问,最开始呢,我们使用 Ado.Net,通过编写 SQL 帮助类帮我们实现对于数据库的快速访问,后来,ORM(Object Relatio ...

  9. ECommon.Dapper 轻量级的dapper扩展

    我们都知道Dapper这个orm框架,但是我们也知道他的扩展目前没有特别好的,今天我就推荐一个轻量级的很方便使用的一个扩展叫做 ECommon.Dapper,它是ECommon的一个类库,关于ECom ...

随机推荐

  1. Stacking方法详解

    集成学习方法主要分成三种:bagging,boosting 和 Stacking.这里主要介绍Stacking. stacking严格来说并不是一种算法,而是精美而又复杂的,对模型集成的一种策略. 首 ...

  2. HTTP 缓存机制详解

    从这里看的 http://mp.weixin.qq.com/s/8UXEMQBkV9hHwtu9R7mV5w

  3. elasticsearch5.2.1使用logstash同步mysql

    centos 本人亲测可以  首先安装好mysql,elasticsearch   不懂的请参考另一篇文章 安装logstash官方:https://www.elastic.co/guide/en/l ...

  4. [Objective-C语言教程]指针(15)

    Objective-C中的指针简单易学.使用指针可以更轻松地执行某些Objective-C编程任务,并且在不使用指针的情况下无法执行其他任务(如动态内存分配). 所以有必要学习指向成为一个完美的Obj ...

  5. [Linux] 如何在 Linux 中提取随机数

    如何在 Linux 中提取随机数 一.设备文件 /dev/random & /dev/urandom 字符特殊文件 /dev/random 和 /dev/urandom (存在于Linux 1 ...

  6. leetcode-78-子集(用bfs解决)

    题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1] ...

  7. window.open完美替代window.showModalDialog

    var url = "http//:www.baidu.com/" var name = "百度"; var iWidth = 1100;//弹窗宽度 var ...

  8. 2016级算法期末模拟练习赛-C.AlvinZH的青春记忆II

    1084 AlvinZH的青春记忆II 思路 中等题,二分. 简化题意,一列数字,每秒会自动-1,特殊操作可以使一个数在1s内-k,问这些数都减至0需要多久. 答案肯定在[1,xMax]之间,采用二分 ...

  9. day3.python 学习之列表

    python中列表用[ ]表示, list =  [ ] #表示一个空列表 1.list = [ 'A','B','C',‘D’] print(list[0]) # 表示打印出列表中的第一个元素,列表 ...

  10. CountDownLatch、CyclicBarrier和 Semaphore

    在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法. 以下 ...