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 = "[email protected]", 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;

}

}

}

}

}

相关推荐
数据智能老司机14 小时前
CockroachDB权威指南——CockroachDB SQL
数据库·分布式·架构
数据智能老司机15 小时前
CockroachDB权威指南——开始使用
数据库·分布式·架构
松果猿15 小时前
空间数据库学习(二)—— PostgreSQL数据库的备份转储和导入恢复
数据库
无名之逆15 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
s91236010115 小时前
rust 同时处理多个异步任务
java·数据库·rust
数据智能老司机15 小时前
CockroachDB权威指南——CockroachDB 架构
数据库·分布式·架构
hzulwy16 小时前
Redis常用的数据结构及其使用场景
数据库·redis
程序猿熊跃晖16 小时前
解决 MyBatis-Plus 中 `update.setProcInsId(null)` 不生效的问题
数据库·tomcat·mybatis
Three~stone17 小时前
MySQL学习集--DDL
数据库·sql·学习
Qi妙代码17 小时前
MYSQL基础
数据库·mysql·oracle