Dapper-OracleSQLHelper 通用封装

/// <summary>

/// Oracle Help 封装使用

/// </summary>

/// <typeparam name="T"></typeparam>

public class OracleSQLDapperHelper<T> where T : class

{

/// <summary>

/// 数据库连接字符串

/// </summary>

/* private static readonly string connectionString =

ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString;*/

private static readonly string connectionString = @"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)

(HOST=192.168.0.139)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=m4mestdb)))

;User ID=yms;Password=yms;";

/// <summary>

/// 查询列表

/// </summary>

/// <param name="sql">查询的sql</param>

/// <param name="param">替换参数</param>

/// <returns></returns>

public static List<T> Query(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.Query<T>(sql, param).ToList();

}

}

/// <summary>

/// 查询第一个数据

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static T QueryFirst(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.QueryFirst<T>(sql, param);

}

}

/// <summary>

/// 查询第一个数据没有返回默认值

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static T QueryFirstOrDefault(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.QueryFirstOrDefault<T>(sql, param);

}

}

/// <summary>

/// 查询单条数据

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static T QuerySingle(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.QuerySingle<T>(sql, param);

}

}

/// <summary>

/// 查询单条数据没有返回默认值

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static T QuerySingleOrDefault(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.QuerySingleOrDefault<T>(sql, param);

}

}

/// <summary>

/// 增删改

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static int Execute(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.Execute(sql, param);

}

}

/// <summary>

/// Reader获取数据

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static IDataReader ExecuteReader(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.ExecuteReader(sql, param);

}

}

/// <summary>

/// Scalar获取数据

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static object ExecuteScalar(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.ExecuteScalar(sql, param);

}

}

/// <summary>

/// Scalar获取数据

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static T ExecuteScalarForT(string sql, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

return con.ExecuteScalar<T>(sql, param);

}

}

/// <summary>

/// 带参数的存储过程

/// </summary>

/// <param name="sql"></param>

/// <param name="param"></param>

/// <returns></returns>

public static List<T> ExecutePro(string proc, object param)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

List<T> list = con.Query<T>(proc,

param,

null,

true,

null,

CommandType.StoredProcedure).ToList();

return list;

}

}

/// <summary>

/// 事务1 - 全SQL

/// </summary>

/// <param name="sqlarr">多条SQL</param>

/// <param name="param">param</param>

/// <returns></returns>

public static int ExecuteTransaction(string[] sqlarr)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

using (var transaction = con.BeginTransaction())

{

try

{

int result = 0;

foreach (var sql in sqlarr)

{

result += con.Execute(sql, null, transaction);

}

transaction.Commit();

return result;

}

catch (Exception ex)

{

transaction.Rollback();

return 0;

}

}

}

}

/// <summary>

/// 事务2 - 声明参数

///demo:

///dic.Add("Insert into Users values (@UserName, @Email, @Address)",

/// new { UserName = "jack", Email = "380234234@qq.com", Address = "上海" });

/// </summary>

/// <param name="Key">多条SQL</param>

/// <param name="Value">param</param>

/// <returns></returns>

public static int ExecuteTransaction(Dictionary<string, object> dic)

{

using (IDbConnection con = new OracleConnection(connectionString))

{

using (var transaction = con.BeginTransaction())

{

try

{

int result = 0;

foreach (var sql in dic)

{

result += con.Execute(sql.Key, sql.Value, transaction);

}

transaction.Commit();

return result;

}

catch (Exception ex)

{

transaction.Rollback();

return 0;

}

}

}

}

}

相关推荐
wowocpp9 分钟前
查看 磁盘文件系统格式 linux ubuntu blkid ext4
linux·数据库·ubuntu
Ai 编码助手6 小时前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
陈燚_重生之又为程序员6 小时前
基于梧桐数据库的实时数据分析解决方案
数据库·数据挖掘·数据分析
caridle6 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
白云如幻6 小时前
MySQL排序查询
数据库·mysql
萧鼎6 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
^velpro^6 小时前
数据库连接池的创建
java·开发语言·数据库
荒川之神6 小时前
ORACLE _11G_R2_ASM 常用命令
数据库·oracle
IT培训中心-竺老师6 小时前
Oracle 23AI创建示例库
数据库·oracle
小白学大数据7 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫