DataManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*******************************************************************************
  2. * Copyright(c) 2014 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DataManager.cs
  5. * 2.功能描述:本系统的数据库访问封装类,所有数据访问都调用
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 欧阳涛 2013/11/21 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Data;
  13. //using System.Data.OracleClient;
  14. using System.Data.SqlClient;
  15. using Oracle.ManagedDataAccess.Client;
  16. namespace Dongke.IBOSS.PRD.Basics.DataAccess
  17. {
  18. public sealed class DataManager
  19. {
  20. //public static string ConnectionString = "Data Source=ORCL;User Id=dongkeTest;Password=dongke;Integrated Security =no;";// 数据库连接串
  21. //public static string ConnectionString = "Provider=OraOLEDB.Oracle;Persist Security Info=False;Data Source=ORCL;User ID=dongkeTest;Password=dongke";
  22. /// <summary>
  23. /// 数据库连接字符串
  24. /// </summary>
  25. public static string ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.19)(PORT=1521))(CONNECT_DATA=(SID=DEVIBOSSPRD)));User Id=deviboss;Password=dongke;";
  26. /// <summary>
  27. /// 数据库连接字符串(报表)
  28. /// </summary>
  29. public static string ConnectionStringReport = "";
  30. /// <summary>
  31. /// 数据库连接字符串
  32. /// </summary>
  33. public static string ConnectionStringFormat = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1}))(CONNECT_DATA=(SERVICE_NAME={2})));User Id={3};Password={4};";
  34. /// <summary>
  35. /// 日志文件保存地址
  36. /// </summary>
  37. public static string LogFileStartPath = System.Windows.Forms.Application.StartupPath;
  38. ///// <summary>
  39. ///// 日志文件名称
  40. ///// </summary>
  41. //public static string LogFileName = "AppLog.log";
  42. ///// <summary>
  43. ///// 日志文件控制类型 True 表示可以写入 False 表示不需要写入
  44. ///// </summary>
  45. //public static bool LogFileControl = true;
  46. public static string LicString = null;
  47. public static DataSet LicDataSet = null;
  48. public static object LicLock = new object();
  49. #region ExecuteNonQuery
  50. /// <summary>
  51. /// 执行SQL语句,返回影响的记录数
  52. /// </summary>
  53. /// <param name="sqlString">SQL语句</param>
  54. /// <returns>
  55. /// 0:SQL语句执行影响的行数为0
  56. /// 大于0:SQL语句执行成功
  57. /// 小于0:SQL语句执行失败
  58. /// </returns>
  59. public static int ExecuteNonQuery(string sqlString)
  60. {
  61. try
  62. {
  63. return OracleHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, sqlString);
  64. }
  65. catch (Exception ex)
  66. {
  67. throw ex;
  68. }
  69. }
  70. /// <summary>
  71. /// 执行带有参数的SQL语句
  72. /// </summary>
  73. /// <param name="strSql">SQL语句</param>
  74. /// <param name="cmdParms">SQL语句的参数</param>
  75. /// <returns>
  76. /// 0:SQL语句执行影响的行数为0
  77. /// 大于0:SQL语句执行成功
  78. /// 小于0:SQL语句执行失败
  79. /// </returns>
  80. public static int ExecuteNonQuery(string sqlString, params OracleParameter[] commandParms)
  81. {
  82. try
  83. {
  84. return OracleHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, sqlString, commandParms);
  85. }
  86. catch (Exception ex)
  87. {
  88. throw ex;
  89. }
  90. }
  91. /// <summary>
  92. /// 用一个已经存在的数据库连接执行一个SQL命令
  93. /// 例如:int result = ExecuteNonQuery("PublishOrders",
  94. /// CommandType.StoredProcedure,
  95. /// new OracleParameter(":prodid", 24));
  96. /// </summary>
  97. /// <param name="commandType">执行命令类型(存储过程、SQL语句等)</param>
  98. /// <param name="commandText">存储过程名称或者SQL语句</param>
  99. /// <param name="commandParameters">参数数组</param>
  100. /// <returns>执行命令对数据库影响的行数</returns>
  101. public static int ExecuteNonQuery(string commandText, CommandType commandType,
  102. params OracleParameter[] commandParameters)
  103. {
  104. try
  105. {
  106. return OracleHelper.ExecuteNonQuery(ConnectionString, commandType, commandText, commandParameters);
  107. }
  108. catch (Exception ex)
  109. {
  110. throw ex;
  111. }
  112. }
  113. /// <summary>
  114. /// 执行多条SQL语句,实现数据库事务。
  115. /// </summary>
  116. /// <param name="sqlStringList">多条SQL语句</param>
  117. /// <returns>
  118. /// -1:执行sql语句失败
  119. /// 其他:执行sql语句成功,并返回影响行数
  120. /// </returns>
  121. public static int ExecuteNonQuery(List<string> sqlStringList)
  122. {
  123. using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
  124. {
  125. // 打开数据库连接
  126. sqlConnection.Open();
  127. // 为执行命令对象赋值
  128. SqlCommand sqlCommand = new SqlCommand();
  129. sqlCommand.Connection = sqlConnection;
  130. SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
  131. sqlCommand.Transaction = sqlTransaction;
  132. int affectedRowCount = 0;
  133. int affectedRowCountTemp = 0;
  134. try
  135. {
  136. // 循环遍历SQL文数据集
  137. foreach (string sqlString in sqlStringList)
  138. {
  139. if (!string.IsNullOrEmpty(sqlString))
  140. {
  141. sqlCommand.CommandText = sqlString;
  142. affectedRowCountTemp = sqlCommand.ExecuteNonQuery();
  143. if (0 > affectedRowCountTemp)
  144. {
  145. sqlTransaction.Rollback();
  146. return -1;
  147. }
  148. affectedRowCount += affectedRowCountTemp;
  149. }
  150. }
  151. sqlTransaction.Commit();
  152. // 清除sql执行对象的属性值
  153. sqlCommand.Parameters.Clear();
  154. sqlCommand.Dispose();
  155. }
  156. catch (SqlException ex)
  157. {
  158. sqlTransaction.Rollback();
  159. throw ex;
  160. }
  161. finally
  162. {
  163. // 释放资源
  164. if (sqlConnection.State != ConnectionState.Closed && sqlConnection != null)
  165. {
  166. sqlConnection.Close();
  167. }
  168. }
  169. return affectedRowCount;
  170. }
  171. }
  172. /// <summary>
  173. /// 执行带参数的多条SQL语句,实现数据库事务。
  174. /// </summary>
  175. /// <param name="sqlStringList">SQL语句列表</param>
  176. /// <param name="commandParmsList">对应的参数列表</param>
  177. /// <returns>
  178. /// -1:执行sql语句失败
  179. /// 其他:执行sql语句成功,并返回影响行数
  180. /// </returns>
  181. public static int ExecuteNonQuery(List<string> sqlStringList, List<OracleParameter[]> commandParmsList)
  182. {
  183. // sql语句的个数与参数个数不相同时,直接返回-1
  184. if (sqlStringList.Count != commandParmsList.Count)
  185. {
  186. return -1;
  187. }
  188. using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
  189. {
  190. // 打开数据库连接
  191. sqlConnection.Open();
  192. // 为执行命令对象赋值
  193. SqlCommand sqlCommand = new SqlCommand();
  194. sqlCommand.Connection = sqlConnection;
  195. SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
  196. sqlCommand.Transaction = sqlTransaction;
  197. int affectedRowCount = 0;
  198. int affectedRowCountTemp = 0;
  199. try
  200. {
  201. // 循环遍历文本和参数
  202. for (int i = 0; i < sqlStringList.Count; i++)
  203. {
  204. if (!string.IsNullOrEmpty(sqlStringList[i]) && commandParmsList[i] != null)
  205. {
  206. // 执行命令的参数设定
  207. foreach (OracleParameter commandParms in commandParmsList[i])
  208. {
  209. if ((commandParms.Direction == ParameterDirection.InputOutput
  210. || commandParms.Direction == ParameterDirection.Input)
  211. && (commandParms.Value == null))
  212. {
  213. commandParms.Value = DBNull.Value;
  214. }
  215. sqlCommand.Parameters.Add(commandParms);
  216. }
  217. // SQL命令的执行语句设定
  218. sqlCommand.CommandText = sqlStringList[i];
  219. // 执行命令
  220. affectedRowCountTemp = sqlCommand.ExecuteNonQuery();
  221. if (0 > affectedRowCountTemp)
  222. {
  223. sqlTransaction.Rollback();
  224. return -1;
  225. }
  226. affectedRowCount += affectedRowCountTemp;
  227. sqlCommand.Parameters.Clear();
  228. }
  229. }
  230. sqlTransaction.Commit();
  231. // 清除sql执行对象的属性值
  232. sqlCommand.Parameters.Clear();
  233. sqlCommand.Dispose();
  234. }
  235. catch (SqlException ex)
  236. {
  237. sqlTransaction.Rollback();
  238. throw ex;
  239. }
  240. finally
  241. {
  242. // 释放资源
  243. if (sqlConnection.State != ConnectionState.Closed && sqlConnection != null)
  244. {
  245. sqlConnection.Close();
  246. }
  247. }
  248. return affectedRowCount;
  249. }
  250. }
  251. #endregion
  252. #region ExecuteDataset
  253. /// <summary>
  254. /// 执行查询语句,返回DataSet
  255. /// </summary>
  256. /// <param name="sqlString">查询语句</param>
  257. /// <returns>DataSet数据集</returns>
  258. public static DataSet ExecuteDataset(string sqlString)
  259. {
  260. try
  261. {
  262. return OracleHelper.Query(ConnectionString, sqlString);
  263. }
  264. catch (Exception ex)
  265. {
  266. throw ex;
  267. }
  268. }
  269. /// <summary>
  270. /// 执行带有参数的查询语句,返回DataSet
  271. /// </summary>
  272. /// <param name="sqlString">查询语句</param>
  273. /// <param name="cmdParms">查询语句的参数</param>
  274. /// <returns>DataSet数据集</returns>
  275. public static DataSet ExecuteDataset(string sqlString, params OracleParameter[] commandParms)
  276. {
  277. try
  278. {
  279. return OracleHelper.Query(ConnectionString, sqlString, commandParms);
  280. }
  281. catch (Exception ex)
  282. {
  283. throw ex;
  284. }
  285. }
  286. #endregion
  287. }
  288. }