智慧大脑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

75 lines
2.4 KiB

  1. using Oracle.ManagedDataAccess.Client;
  2. using System.Configuration;
  3. using System.Data;
  4. namespace DataSendApi.Program.Oracle
  5. {
  6. public class DapperHelper
  7. {
  8. /// 数据库连接名
  9. private static string _connection = string.Empty;
  10. /// 获取连接名
  11. private static string Connection
  12. {
  13. get { return _connection; }
  14. //set { _connection = value; }
  15. }
  16. /// 返回连接实例
  17. private static IDbConnection dbConnection = null;
  18. /// 静态变量保存类的实例
  19. private static DapperHelper uniqueInstance;
  20. /// 定义一个标识确保线程同步
  21. private static readonly object locker = new object();
  22. /// <summary>
  23. /// 私有构造方法,使外界不能创建该类的实例,以便实现单例模式
  24. /// </summary>
  25. private DapperHelper()
  26. {
  27. // 这里为了方便演示直接写的字符串,实例项目中可以将连接字符串放在配置文件中,再进行读取。
  28. _connection = ConfigurationManager.AppSettings["OracleConnectionString"];
  29. }
  30. /// <summary>
  31. /// 获取实例,这里为单例模式,保证只存在一个实例
  32. /// </summary>
  33. /// <returns></returns>
  34. public static DapperHelper GetInstance()
  35. {
  36. // 双重锁定实现单例模式,在外层加个判空条件主要是为了减少加锁、释放锁的不必要的损耗
  37. if (uniqueInstance == null)
  38. {
  39. lock (locker)
  40. {
  41. if (uniqueInstance == null)
  42. {
  43. uniqueInstance = new DapperHelper();
  44. }
  45. }
  46. }
  47. return uniqueInstance;
  48. }
  49. /// <summary>
  50. /// 创建数据库连接对象并打开链接
  51. /// </summary>
  52. /// <returns></returns>
  53. public static IDbConnection OpenCurrentDbConnection()
  54. {
  55. if (dbConnection == null)
  56. {
  57. dbConnection = new OracleConnection(Connection);
  58. }
  59. //判断连接状态
  60. if (dbConnection.State == ConnectionState.Closed)
  61. {
  62. dbConnection.Open();
  63. }
  64. return dbConnection;
  65. }
  66. }
  67. }