OracleHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. using Common;
  2. using Npgsql;
  3. using Oracle.ManagedDataAccess.Client;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Reflection;
  9. namespace LisPacsDataUpload
  10. {
  11. public class OracleHelper<T> where T : class
  12. {
  13. //数据库连接变量
  14. //private static OracleConnection conn = null;
  15. //数据库事务变量
  16. //private static OracleTransaction transaction = null;
  17. //连接字符串
  18. public static string GetConnStr()
  19. {
  20. return ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
  21. }
  22. //连接字符串
  23. public static string GetPgConnStr()
  24. {
  25. return ConfigurationManager.ConnectionStrings["pgConn"].ConnectionString;
  26. }
  27. /// <summary>
  28. /// 轮询间隔时间
  29. /// </summary>
  30. /// <returns></returns>
  31. public static int Time()
  32. {
  33. return Convert.ToInt32(ConfigurationManager.AppSettings["time"]) * 60 * 1000;
  34. }
  35. /// <summary>
  36. /// 数据上传开始时间
  37. /// </summary>
  38. /// <returns></returns>
  39. public static int StartTime()
  40. {
  41. return Convert.ToInt32(ConfigurationManager.AppSettings["start_time"]);
  42. }
  43. public static int UploadPacs()
  44. {
  45. return Convert.ToInt32(ConfigurationManager.AppSettings["pacs"]);
  46. }
  47. public static int UploadTime()
  48. {
  49. return Convert.ToInt32(ConfigurationManager.AppSettings["upload_time"]);
  50. }
  51. //public static OracleConnection getConnection()
  52. //{
  53. // OracleConnection conn = null;
  54. // if (conn == null || conn.State == ConnectionState.Closed)
  55. // openConnection();
  56. // return conn;
  57. //}
  58. private static object openLock = new object();
  59. //打开数据库连接
  60. private static bool openConnection(OracleConnection conn)
  61. {
  62. lock (openLock)
  63. {
  64. try
  65. {
  66. conn = new OracleConnection(GetConnStr());
  67. conn.Close();
  68. if (conn.State != ConnectionState.Open)
  69. {
  70. conn.Open();
  71. return true;
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. LogHelper.Info("打开数据库出错," + ex.Message);
  77. }
  78. finally { }
  79. return false;
  80. }
  81. //lock (openLock)
  82. //{
  83. // try
  84. // {
  85. // //打开连接
  86. // if (conn == null || conn.State == ConnectionState.Closed)
  87. // {
  88. // conn = new OracleConnection(GetConnStr());
  89. // conn.Open();
  90. // }
  91. // }
  92. // catch (Exception ex)
  93. // {
  94. // LogHelper.Error(ex, ex.Message);
  95. // //throw new Exception(ex.Message, ex);
  96. // }
  97. }
  98. private static object closeLock = new object();
  99. //关闭数据库连接
  100. private static void closeConnection()
  101. {
  102. lock (closeLock)
  103. {
  104. OracleConnection conn = null;
  105. if (conn != null)
  106. {
  107. if (conn.State == ConnectionState.Open)
  108. conn.Close();
  109. }
  110. }
  111. //lock(closeLock)
  112. //{
  113. // try
  114. // {
  115. // if (conn != null)
  116. // {
  117. // if (conn.State == ConnectionState.Open)
  118. // conn.Close();
  119. // }
  120. // }
  121. // catch (Exception ex)
  122. // {
  123. // LogHelper.Error(ex, ex.Message);
  124. // //throw new Exception(ex.Message, ex);
  125. // }
  126. //}
  127. }
  128. private static object DSLock = new object();
  129. /// <summary>
  130. /// 查询,返回DataSet
  131. /// </summary>
  132. /// <param name="sql"></param>
  133. /// <returns></returns>
  134. public static DataTable RunQueryDS(string sql)
  135. {
  136. //lock (DSLock)
  137. //{
  138. DataTable dt = new DataTable();
  139. using (OracleConnection conn = new OracleConnection(GetConnStr()))
  140. {
  141. try
  142. {
  143. conn.Open();
  144. OracleDataAdapter oda = new OracleDataAdapter(sql, conn);
  145. //DataSet ds = new DataSet();
  146. oda.Fill(dt);
  147. //dt = ds.Tables[0];
  148. }
  149. catch (Exception ex)
  150. {
  151. LogHelper.Info("查询出错sql:" + sql);
  152. LogHelper.Error(ex, ex.Message);
  153. //throw new Exception(ex.Message);
  154. }
  155. finally
  156. {
  157. //closeConnection();
  158. conn.Close();
  159. }
  160. }
  161. return dt;
  162. //}
  163. }
  164. /// <summary>
  165. /// 查询,返回DataSet
  166. /// </summary>
  167. /// <param name="sql"></param>
  168. /// <returns></returns>
  169. public static DataTable PgQueryDS(string sql)
  170. {
  171. DataTable dataTable = new DataTable();
  172. using (NpgsqlConnection connection = new NpgsqlConnection(GetPgConnStr()))
  173. {
  174. connection.Open();
  175. using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
  176. {
  177. using (NpgsqlDataReader reader = command.ExecuteReader())
  178. {
  179. dataTable.Load(reader);
  180. }
  181. }
  182. }
  183. return dataTable;
  184. }
  185. /// <summary>
  186. /// 执行存储过程
  187. /// </summary>
  188. /// <param name="procName">存储过程名</param>
  189. /// <param name="commandParameters">参数列表</param>
  190. public static void ExecuteProcedure(string procName, params OracleParameter[] commandParameters)
  191. {
  192. //initConnection();
  193. //initTransaction();
  194. try
  195. {
  196. OracleConnection conn = null;
  197. //执行命令
  198. OracleCommand cmd = new OracleCommand();
  199. cmd.Connection = conn;
  200. cmd.CommandText = procName;
  201. cmd.CommandType = CommandType.StoredProcedure;
  202. // 绑定传入的参数
  203. if (commandParameters != null)
  204. {
  205. foreach (OracleParameter parm in commandParameters)
  206. {
  207. cmd.Parameters.Add(parm);
  208. }
  209. }
  210. cmd.ExecuteNonQuery();
  211. //commitTransaction();
  212. cmd.Parameters.Clear();
  213. }
  214. catch (Exception ex)
  215. {
  216. //rollbackTransaction();
  217. LogHelper.Error(ex, ex.Message);
  218. throw new Exception(ex.Message, ex);
  219. }
  220. finally
  221. {
  222. //关闭数据库连接
  223. closeConnection();
  224. }
  225. }
  226. private static object ExtObjectLock = new object();
  227. /// <summary>
  228. /// 执行一个update,insert,delete,drop等SQL
  229. /// </summary>
  230. /// <param name="cmdText">SQL值</param>
  231. /// <returns>执行是否成功</returns>
  232. public static int ExecuteNonQuery(string sql)
  233. {
  234. //lock (ExtObjectLock)
  235. //{
  236. int val = 0;
  237. //initTransaction();
  238. OracleTransaction tran = null;
  239. using (OracleConnection conn = new OracleConnection(GetConnStr()))
  240. {
  241. //创建连接
  242. try
  243. {
  244. conn.Open();
  245. if (conn.State != ConnectionState.Connecting)
  246. {
  247. bool isb = false;
  248. try
  249. {
  250. tran = conn.BeginTransaction();
  251. isb = true;
  252. }
  253. catch
  254. { }
  255. finally
  256. {
  257. if (isb)
  258. {
  259. //创建一个新的命令
  260. OracleCommand cmd = new OracleCommand();
  261. cmd.CommandType = CommandType.Text;
  262. cmd.Connection = conn;
  263. cmd.Transaction = tran;
  264. //cmd.Connection = tran.Connection;
  265. cmd.CommandText = sql;
  266. //执行命令
  267. val = cmd.ExecuteNonQuery();
  268. tran.Commit();
  269. }
  270. }
  271. }
  272. }
  273. catch (System.Exception ex)
  274. {
  275. val = 0;
  276. //rollbackTransaction();
  277. LogHelper.Info("执行出错sql:" + sql);
  278. LogHelper.Error(ex, ex.Message);
  279. if (tran != null) { tran.Rollback(); tran.Dispose(); tran = null; }
  280. }
  281. finally
  282. {
  283. tran.Dispose();
  284. //关闭数据库连接
  285. //closeConnection();
  286. conn.Close();
  287. }
  288. }
  289. return val;
  290. //}
  291. }
  292. public static int PgExecuteNonQuery(string query)
  293. {
  294. int newId;
  295. try {
  296. using (var conn = new NpgsqlConnection(GetPgConnStr()))
  297. {
  298. using (var command = new NpgsqlCommand(query, conn))
  299. {
  300. conn.Open();
  301. newId = Convert.ToInt32(command.ExecuteScalar());
  302. }
  303. }
  304. }
  305. catch (Exception e) {
  306. LogHelper.Info(e.Message + e.InnerException);
  307. return 0;
  308. }
  309. //, params object[] parameters 这是入参 暂时不用
  310. return newId;
  311. }
  312. /// <summary>
  313. /// 将DataSet转换为泛型集合List
  314. /// </summary>
  315. /// <param name="ds">要转换的DataSet数据集</param>
  316. /// <returns>转换之后的泛型集合</returns>
  317. private static List<T> DataSetToList(DataSet ds)
  318. {
  319. List<T> list = new List<T>();
  320. try
  321. {
  322. // 根据数据量进行遍历
  323. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  324. {
  325. // 通过类型创建该类型的实例
  326. T _t = (T)Activator.CreateInstance(typeof(T));
  327. // 通过创建的实例获取该实例下的所有公共字段
  328. PropertyInfo[] propertys = _t.GetType().GetProperties();
  329. // 使用嵌套循环遍历数据列,匹配数据列名称与获取的公共字段名称,对符合的字段进行赋值
  330. foreach (PropertyInfo pi in propertys)
  331. {
  332. for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
  333. {
  334. if (pi.Name.Equals(ds.Tables[0].Columns[j].ColumnName))
  335. {
  336. //LogHelper.Info("666:" + ds.Tables[0].Columns[j].ColumnName);
  337. if (ds.Tables[0].Rows[i][j] != DBNull.Value)
  338. pi.SetValue(_t, ds.Tables[0].Rows[i][j], null);
  339. else
  340. // 对于数据库中的Null值需要进行单独处理
  341. pi.SetValue(_t, "", null);
  342. }
  343. }
  344. }
  345. list.Add(_t);
  346. }
  347. }
  348. catch (Exception ex)
  349. {
  350. // 抛出异常,上层进行异常捕获和处理并记录错误日志
  351. throw new Exception(ex.Message);
  352. }
  353. return list;
  354. }
  355. private static object ListLock = new object();
  356. /// <summary>
  357. /// 查询操作(返回泛型集合)当有查询操作时,应优先考虑使用RunQueryList方法
  358. /// </summary>
  359. /// <param name="sql">要执行的查询语句</param>
  360. /// <returns>对应数据实体的泛型集合</returns>
  361. public static List<T> RunQueryList(string sql)
  362. {
  363. //lock (ListLock)
  364. //{
  365. List<T> list = new List<T>();
  366. using (OracleConnection conn = new OracleConnection(GetConnStr()))
  367. {
  368. try
  369. {
  370. conn.Open();
  371. DataSet ds = new DataSet();
  372. OracleDataAdapter oda = new OracleDataAdapter(sql, conn);
  373. oda.Fill(ds);
  374. // 若没有查询到任何数据,则返回空
  375. if (ds == null || ds.Tables.Count < 0)
  376. return null;
  377. // 调用DataSet转List的转换方法
  378. list = DataSetToList(ds);
  379. }
  380. catch (Exception ex)
  381. {
  382. //conn.Close();
  383. LogHelper.Error(ex, ex.Message);
  384. LogHelper.Info("查询出错sql:" + sql);
  385. //throw new Exception(ex.Message);
  386. }
  387. finally
  388. {
  389. conn.Close();
  390. //closeConnection();
  391. }
  392. }
  393. return list;
  394. //}
  395. }
  396. public static List<T> PgRunQueryList(string sql)
  397. {
  398. List<T> ts = new List<T>();
  399. try
  400. {
  401. using (var conn = new NpgsqlConnection(GetPgConnStr()))
  402. {
  403. if (conn.State != ConnectionState.Open)
  404. {
  405. conn.Open();
  406. }
  407. T t = default(T);
  408. using (var nda = new NpgsqlDataAdapter(sql, conn))
  409. {
  410. DataTable dt = new DataTable();
  411. nda.Fill(dt);
  412. // 获得此模型的类型
  413. // Type type = typeof(McDevice);
  414. string tempName = "";
  415. foreach (DataRow dr in dt.Rows)
  416. {
  417. t = Activator.CreateInstance<T>();
  418. // 获得此模型的公共属性
  419. PropertyInfo[] propertys = t.GetType().GetProperties();
  420. foreach (PropertyInfo pi in propertys)
  421. {
  422. tempName = pi.Name; // 检查DataTable是否包含此列
  423. if (dt.Columns.Contains(tempName))
  424. {
  425. // 判断此属性是否有Setter
  426. if (!pi.CanWrite) continue;
  427. object value = dr[tempName];
  428. if (value != DBNull.Value)
  429. pi.SetValue(t, value, null);
  430. }
  431. }
  432. ts.Add(t);
  433. }
  434. }
  435. }
  436. }
  437. catch (Exception ex) { LogHelper.Info("查询pg数据返回lis集合异常" + ex.Message); }
  438. return ts;
  439. }
  440. }
  441. }