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 }
}