using Dapper;
using DapperExtensions;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace DBLibrarys.DBHelper
{
///
/// 数据库操作类
///
public class SqlDapperHelper
{
static string connStrRead = ConfigurationManager.ConnectionStrings["Read"].ConnectionString;
static string connStrWrite = ConfigurationManager.ConnectionStrings["Write"].ConnectionString;
static int commandTimeout = 30;
public static IDbConnection GetConnection(bool useWriteConn)
{
if (useWriteConn)
return new SqlConnection(connStrWrite);
return new SqlConnection(connStrRead);
}
public static SqlConnection GetOpenConnection()
{
var conn = new SqlConnection(connStrWrite);
conn.Open();
return conn;
}
///
/// 执行sql返回一个对象
///
///
///
///
///
///
public static T ExecuteReaderReturnT
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return conn.QueryFirstOrDefault
}
}
else
{
var conn = transaction.Connection;
return conn.QueryFirstOrDefault
}
}
///
/// 执行sql返回多个对象
///
///
///
///
///
///
public static List
{
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return conn.Query
}
}
///
/// 执行sql返回一个对象--异步
///
///
///
///
///
///
public static async Task
{
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return await conn.QueryFirstOrDefaultAsync
}
}
///
/// 执行sql返回多个对象--异步
///
///
///
///
///
///
public static async Task> ExecuteReaderRetListAsync
{
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
var list = await conn.QueryAsync
return list.ToList();
}
}
///
/// 执行sql,返回影响行数
///
///
///
///
///
public static int ExecuteSqlInt(string sql, object param = null, IDbTransaction transaction = null)
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
return conn.Execute(sql, param, commandTimeout: commandTimeout, commandType: CommandType.Text);
}
}
else
{
var conn = transaction.Connection;
return conn.Execute(sql, param, transaction: transaction, commandTimeout: commandTimeout, commandType: CommandType.Text);
}
}
///
/// 执行sql,返回影响行数--异步
///
///
///
///
///
public static async Task
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
return await conn.ExecuteAsync(sql, param, commandTimeout: commandTimeout, commandType: CommandType.Text).ConfigureAwait(false);
}
}
else
{
var conn = transaction.Connection;
return await conn.ExecuteAsync(sql, param, transaction: transaction, commandTimeout: commandTimeout, commandType: CommandType.Text).ConfigureAwait(false);
}
}
///
/// 根据id获取实体
///
///
///
///
///
///
public static T GetById
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return conn.Get
}
}
else
{
var conn = transaction.Connection;
return conn.Get
}
}
///
/// 根据id获取实体--异步
///
///
///
///
///
///
public static async Task
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return await conn.GetAsync
}
}
else
{
var conn = transaction.Connection;
return await conn.GetAsync
}
}
///
/// 插入实体
///
///
///
///
///
public static string ExecuteInsert
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
var res = conn.Insert
return res;
}
}
else
{
var conn = transaction.Connection;
return conn.Insert(item, transaction: transaction, commandTimeout: commandTimeout);
}
}
///
/// 批量插入实体
///
///
///
///
public static void ExecuteInsertList
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
conn.Insert
}
}
else
{
var conn = transaction.Connection;
conn.Insert(list, transaction: transaction, commandTimeout: commandTimeout);
}
}
///
/// 更新单个实体
///
///
///
///
///
public static bool ExecuteUpdate
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
return conn.Update(item, commandTimeout: commandTimeout);
}
}
else
{
var conn = transaction.Connection;
return conn.Update(item, transaction: transaction, commandTimeout: commandTimeout);
}
}
///
/// 批量更新实体
///
///
///
///
///
public static bool ExecuteUpdateList
{
if (transaction == null)
{
using (IDbConnection conn = GetConnection(true))
{
conn.Open();
return conn.Update(item, commandTimeout: commandTimeout);
}
}
else
{
var conn = transaction.Connection;
return conn.Update(item, transaction: transaction, commandTimeout: commandTimeout);
}
}
///
/// 分页查询
///
///
/// 主sql 不带 order by
/// 排序内容 id desc,add_time asc
/// 第几页
/// 每页多少条
/// 是否主库
///
public static List
{
string pageSql = @"SELECT TOP {0} * FROM (SELECT ROW_NUMBER() OVER (ORDER BY {1}) _row_number_,* FROM
({2})temp )temp1 WHERE temp1._row_number_>{3} ORDER BY _row_number_";
string execSql = string.Format(pageSql, pageSize, sort, sql, pageSize * (pageIndex - 1));
using (IDbConnection conn = GetConnection(useWriteConn))
{
conn.Open();
return conn.Query
}
}
}
}
https://www.cnblogs.com/feizianquan/p/9786822.html