发布:2023/12/7 19:20:13作者:管理员 来源:本站 浏览次数:390
internal class DapperHelper
{
//使用单例模式
private static DapperHelper _Singleton = null;
static SqlSugar.DbType dbtype = SqlSugar.DbType.SqlServer;
static string ConnectionString = string.Empty;
private static object Singleton_Lock = new object();
public static DapperHelper Init(SqlSugar.DbType dbtype = SqlSugar.DbType.SqlServer)
{
if (_Singleton == null) //双if +lock
{
lock (Singleton_Lock)
{
if (_Singleton == null)
{
//创建对象
_Singleton = new DapperHelper(dbtype);
}
}
}
return _Singleton;
}
public DapperHelper(SqlSugar.DbType dbtype)
{
if (dbtype == SqlSugar.DbType.SqlServer)
{
DapperHelper.ConnectionString = ConnTools.init().GetSqlServerConn();
}
else if (dbtype == SqlSugar.DbType.Oracle)
{
DapperHelper.ConnectionString = ConnTools.init().GetOracleConn();
}
DapperHelper.dbtype = dbtype;
}
/// <summary>
/// 添加.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入实体类型.</param>
/// <returns>int.</returns>
public int Add<T>(string sql, T t)
where T : class
{
return Add<T>(sql, t,ConnectionString);
}
/// <summary>
/// 添加.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入实体类型.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>int.</returns>
public int Add<T>(string sql, T t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Execute(sql, t);
}
}
/// <summary>
/// 批量添加.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入泛型类.</param>
/// <returns>int.</returns>
public int Add<T>(string sql, List<T> t)
where T : class
{
return Add<T>(sql, t, ConnectionString);
}
/// <summary>
/// 批量添加.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入泛型类.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>int.</returns>
public int Add<T>(string sql, List<T> t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Execute(sql, t);
}
}
/// <summary>
/// 删除.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入实体类型.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>int.</returns>
public int Delete<T>(string sql, T t)
where T : class
{
return Delete<T>(sql, t, ConnectionString)
; }
/// <summary>
/// 删除.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入实体类型.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>int.</returns>
public int Delete<T>(string sql, T t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Execute(sql, t);
}
}
/// <summary>
/// 批量删除.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入泛型类.</param>
/// <returns>int.</returns>
public int Delete<T>(string sql, List<T> t)
where T : class
{
return Delete<T>(sql, t, ConnectionString);
}
/// <summary>
/// 批量删除.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入泛型类.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>int.</returns>
public int Delete<T>(string sql, List<T> t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Execute(sql, t);
}
}
/// <summary>
/// 修改.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入实体类型.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>int.</returns>
public int Update<T>(string sql, T t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Execute(sql, t);
}
}
/// <summary>
/// 批量修改.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入泛型类.</param>
/// <returns>int.</returns>
public int Update<T>(string sql, List<T> t)
where T : class
{
return Update<T>(sql, t, ConnectionString);
}
/// <summary>
/// 批量修改.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入泛型类.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>int.</returns>
public int Update<T>(string sql, List<T> t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Execute(sql, t);
}
}
/// <summary>
/// 查询.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <returns>泛型类.</returns>
public List<T> Query<T>(string sql)
where T : class
{
return Query<T>(sql, ConnectionString);
}
/// <summary>
/// 查询.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>泛型类.</returns>
public List<T> Query<T>(string sql, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Query<T>(sql).ToList();
}
}
/// <summary>
/// 查询指定数据.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入泛型类.</param>
/// <returns>类.</returns>
public T Query<T>(string sql, T t)
where T : class
{
return Query<T>(sql, t, ConnectionString);
}
/// <summary>
/// 查询指定数据.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="t">传入泛型类.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>类.</returns>
public T Query<T>(string sql, T t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Query<T>(sql, t).SingleOrDefault();
}
}
/// <summary>
/// 查询多个数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="t"></param>
/// <returns></returns>
public List<T> QueryList<T>(string sql, T t)
where T : class
{
return QueryList<T>(sql, t, ConnectionString);
}
/// <summary>
/// 查询多条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql">查询语句</param>
/// <param name="t">模型</param>
/// <param name="ConnectionString">数据库连接字符串</param>
/// <returns></returns>
public List<T> QueryList<T>(string sql, T t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Query<T>(sql, t).ToList();
}
}
/// <summary>
/// 查询数据返回datatable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="t"></param>
/// <returns></returns>
public DataTable QueryDataTable<T>(string sql, T t)
where T : class
{
return QueryDataTable<T>(sql, t, ConnectionString);
}
public DataTable QueryDataTable<T>(string sql, T t, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Query<DataTable>(sql, t).SingleOrDefault();
}
} /// <summary>
/// 查询的in操作.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <returns>泛型类.</returns>
public List<T> Query<T>(string sql, int[] ids)
where T : class
{
return Query<T>(sql, ids, ConnectionString)
; }
/// <summary>
/// 查询的in操作.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="ConnectionString">链接字符串.</param>
/// <returns>泛型类.</returns>
public List<T> Query<T>(string sql, int[] ids, string ConnectionString)
where T : class
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
return connection.Query<T>(sql, new { ids }).ToList();
}
}
/// <summary>
/// 多语句操作.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="ConnectionString">链接字符串.</param>
public void QueryMultiple<T>(string sql, T t)
{
return QueryMultiple<T>(sql, t, ConnectionString);
}
/// <summary>
/// 多语句操作.
/// </summary>
/// <typeparam name="T">实体类型.</typeparam>
/// <param name="sql">传入sql执行语句.</param>
/// <param name="ConnectionString">链接字符串.</param>
public void QueryMultiple<T>(string sql, T t, string ConnectionString)
{
using (IDbConnection connection = new SqlConnection(ConnectionString))
{
var multiReader = connection.QueryMultiple(sql);
var userInfo = multiReader.Read<T>();
multiReader.Dispose();
}
}
}
}
代码中用到的枚举
public enum DbType
{
MySql = 0,
SqlServer = 1,
Sqlite = 2,
Oracle = 3,
PostgreSQL = 4,
Dm = 5,
Kdbndp = 6,
Oscar = 7,
MySqlConnector = 8,
Access = 9,
Custom = 900
}