MSSQLServerDBTransaction.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:MSSQLServerDBTransaction.cs
  5. * 2.功能描述:SQL数据库带有事物的操作类
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 张国印 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Data;
  13. using System.Data.SqlClient;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. namespace Dongke.IBOSS.PRD.Basics.DataAccess
  18. {
  19. /// <summary>
  20. /// 数据库带有事物的操作类
  21. /// </summary>
  22. public class MSSQLServerDBTransaction : IDBTransaction
  23. {
  24. private SqlConnection m_SqlConnection;
  25. private SqlTransaction m_SqlTransaction;
  26. private ConnectionState m_ConnState;
  27. private string m_connStr;
  28. /// <summary>
  29. /// 事务是否自动提交
  30. /// </summary>
  31. public bool m_AutoCommit = false;
  32. /// <summary>
  33. /// 忽略大小写
  34. /// </summary>
  35. public bool IgnoreCase
  36. {
  37. get;
  38. set;
  39. }
  40. /// <summary>
  41. /// SQL执行限定超时
  42. /// </summary>
  43. public bool IsCommandTimeout
  44. {
  45. get;
  46. set;
  47. }
  48. public MSSQLServerDBTransaction(string m_strConn)
  49. {
  50. try
  51. {
  52. m_connStr = m_strConn;
  53. }
  54. catch (Exception ex)
  55. {
  56. string strError = ex.Message + ex.Source;
  57. throw new Exception(strError, ex);
  58. }
  59. }
  60. #region IDBTransaction 成员
  61. public IDbConnection DbConnection
  62. {
  63. get
  64. {
  65. return m_SqlConnection;
  66. }
  67. set
  68. {
  69. m_SqlConnection = (value as SqlConnection);
  70. }
  71. }
  72. public bool AutoCommit
  73. {
  74. get
  75. {
  76. return m_AutoCommit;
  77. }
  78. set
  79. {
  80. m_AutoCommit = value;
  81. }
  82. }
  83. public ConnectionState ConnState
  84. {
  85. get
  86. {
  87. return m_ConnState;
  88. }
  89. set
  90. {
  91. m_ConnState = value;
  92. }
  93. }
  94. public string ConnStr
  95. {
  96. get { return m_connStr; }
  97. }
  98. public ConnectionState Connect()
  99. {
  100. try
  101. {
  102. // 初始化连接
  103. m_SqlConnection = new SqlConnection(m_connStr);
  104. m_SqlConnection.Open();
  105. m_ConnState = m_SqlConnection.State;
  106. m_SqlTransaction = m_SqlConnection.BeginTransaction(IsolationLevel.ReadCommitted);
  107. }
  108. catch (Exception ex)
  109. {
  110. m_ConnState = ConnectionState.Closed;
  111. string strError = ex.Message + ex.Source;
  112. throw new Exception(strError, ex);
  113. }
  114. return m_ConnState;
  115. }
  116. public bool CreateTransaction()
  117. {
  118. bool blSucess = false;
  119. try
  120. {
  121. if (m_ConnState != ConnectionState.Open)
  122. Connect();
  123. m_SqlTransaction = m_SqlConnection.BeginTransaction(IsolationLevel.ReadCommitted);
  124. blSucess = true;
  125. }
  126. catch (Exception ex)
  127. {
  128. string strError = ex.Message + ex.Source;
  129. throw new Exception(strError, ex);
  130. }
  131. return blSucess;
  132. }
  133. public ConnectionState Disconnect()
  134. {
  135. try
  136. {
  137. if (m_ConnState == ConnectionState.Open)
  138. m_SqlConnection.Close();
  139. m_ConnState = m_SqlConnection.State;
  140. }
  141. catch (Exception ex)
  142. {
  143. m_ConnState = ConnectionState.Closed;
  144. string strError = ex.Message + ex.Source;
  145. throw new Exception(strError, ex);
  146. }
  147. return m_ConnState;
  148. }
  149. public void Commit()
  150. {
  151. try
  152. {
  153. m_SqlTransaction.Commit();
  154. }
  155. catch (Exception ex)
  156. {
  157. string strError = ex.Message + ex.Source;
  158. throw new Exception(strError, ex);
  159. }
  160. }
  161. public void Rollback()
  162. {
  163. try
  164. {
  165. m_SqlTransaction.Rollback();
  166. }
  167. catch (Exception ex)
  168. {
  169. string strError = ex.Message + ex.Source;
  170. throw new Exception(strError, ex);
  171. }
  172. }
  173. public void RollbackTo(string p_strcheckPoint)
  174. {
  175. //try
  176. //{
  177. // m_SqlTransaction.Rollback();
  178. //}
  179. //catch (Exception ex)
  180. //{
  181. // string strError = ex.Message + ex.Source;
  182. // throw new Exception(strError, ex);
  183. //}
  184. m_SqlTransaction.Rollback(p_strcheckPoint);
  185. }
  186. public void SavePoint(string p_strcheckPoint)
  187. {
  188. //throw new Exception("不支持此方法!");
  189. m_SqlTransaction.Save(p_strcheckPoint);
  190. }
  191. public int ExecuteNonQuery(string p_strCommand, IDataParameter[] p_Parameter)
  192. {
  193. return ExecuteNonQuery(p_strCommand, false, p_Parameter);
  194. }
  195. public int ExecuteNonQuery(string p_strCommand, bool p_procedure, IDataParameter[] p_Parameter)
  196. {
  197. int blReturn = 0;
  198. if (m_ConnState != ConnectionState.Open)
  199. Connect();
  200. try
  201. {
  202. SqlCommand myCommand = new SqlCommand(p_strCommand, m_SqlConnection);
  203. if (p_Parameter != null)
  204. {
  205. myCommand.Parameters.AddRange(p_Parameter);
  206. }
  207. myCommand.Transaction = m_SqlTransaction;
  208. if (p_procedure)
  209. {
  210. myCommand.CommandType = CommandType.StoredProcedure;
  211. }
  212. else
  213. {
  214. myCommand.CommandType = CommandType.Text;
  215. }
  216. blReturn = myCommand.ExecuteNonQuery();
  217. if (m_AutoCommit)
  218. Commit();
  219. }
  220. catch (Exception ex)
  221. {
  222. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  223. throw new Exception(strError, ex);
  224. }
  225. return blReturn;
  226. }
  227. public int UpdateBlob(string p_strTable, string p_strColumn, byte[] p_blobData, string p_strWhere)
  228. {
  229. int intReturn = 0;
  230. string strCommand = "UPDATE " + p_strTable + " SET " + p_strColumn + " =@blobValue where " + p_strWhere;
  231. if (m_SqlConnection.State != ConnectionState.Open)
  232. Connect();
  233. try
  234. {
  235. SqlCommand myCommand = new SqlCommand(strCommand, m_SqlConnection);
  236. myCommand.Transaction = m_SqlTransaction;
  237. SqlParameter aseParam = new SqlParameter("@blobValue", SqlDbType.Image);
  238. aseParam.Direction = ParameterDirection.InputOutput;
  239. aseParam.Value = p_blobData;
  240. myCommand.Parameters.Add(aseParam);
  241. intReturn = myCommand.ExecuteNonQuery();
  242. if (m_AutoCommit)
  243. Commit();
  244. intReturn = 1;
  245. }
  246. catch (Exception ex)
  247. {
  248. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + strCommand;
  249. throw new Exception(strError, ex);
  250. }
  251. return intReturn;
  252. }
  253. public DataSet GetSqlResultToDs(string p_strCommand, IDataParameter[] p_Parameter)
  254. {
  255. DataSet myDs = new DataSet();
  256. if (m_SqlConnection.State != ConnectionState.Open)
  257. Connect();
  258. try
  259. {
  260. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  261. if (p_Parameter != null)
  262. {
  263. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  264. }
  265. dataAdapter.SelectCommand.Transaction = m_SqlTransaction;
  266. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  267. dataAdapter.Fill(myDs);
  268. }
  269. catch (Exception ex)
  270. {
  271. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  272. throw new Exception(strError, ex);
  273. }
  274. return myDs;
  275. }
  276. public DataSet GetSqlResultToDs(string p_strCommand, string p_strName, IDataParameter[] p_Parameter)
  277. {
  278. DataSet myDs = new DataSet();
  279. if (m_SqlConnection.State != ConnectionState.Open)
  280. Connect();
  281. try
  282. {
  283. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  284. if (p_Parameter != null)
  285. {
  286. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  287. }
  288. dataAdapter.SelectCommand.Transaction = m_SqlTransaction;
  289. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  290. dataAdapter.Fill(myDs, p_strName);
  291. }
  292. catch (Exception ex)
  293. {
  294. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  295. throw new Exception(strError, ex);
  296. }
  297. return myDs;
  298. }
  299. public DataTable GetSqlResultToDt(string p_strCommand, IDataParameter[] p_Parameter)
  300. {
  301. //初始化结果集(DT)
  302. DataTable myDt = new DataTable();
  303. if (m_SqlConnection.State != ConnectionState.Open)
  304. Connect();
  305. try
  306. {
  307. //dataAdapte实例化
  308. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  309. if (p_Parameter != null)
  310. {
  311. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  312. }
  313. dataAdapter.SelectCommand.Transaction = m_SqlTransaction;
  314. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  315. //填充数据集(DT)
  316. dataAdapter.Fill(myDt);
  317. }
  318. catch (Exception ex)
  319. {
  320. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  321. throw new Exception(strError, ex);
  322. }
  323. //返回数据集
  324. return myDt;
  325. }
  326. public string GetSqlResultToStr(string p_strCommand, IDataParameter[] p_Parameter)
  327. {
  328. string strResult = "";
  329. object retValue = null;
  330. if (m_ConnState != ConnectionState.Open)
  331. Connect();
  332. try
  333. {
  334. SqlCommand oraCmd = new SqlCommand(p_strCommand, m_SqlConnection);
  335. if (p_Parameter != null)
  336. {
  337. oraCmd.Parameters.AddRange(p_Parameter);
  338. }
  339. oraCmd.Transaction = m_SqlTransaction;
  340. oraCmd.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  341. retValue = oraCmd.ExecuteScalar();
  342. if (retValue != null)
  343. strResult = retValue.ToString();
  344. else
  345. strResult = String.Empty;
  346. }
  347. catch (Exception ex)
  348. {
  349. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  350. throw new Exception(strError, ex);
  351. }
  352. return strResult;
  353. }
  354. public object GetSqlResultToObj(string p_strCommand, IDataParameter[] p_Parameter)
  355. {
  356. object retValue = null;
  357. if (m_ConnState != ConnectionState.Open)
  358. Connect();
  359. try
  360. {
  361. SqlCommand oraCmd = new SqlCommand(p_strCommand, m_SqlConnection);
  362. if (p_Parameter != null)
  363. {
  364. oraCmd.Parameters.AddRange(p_Parameter);
  365. }
  366. oraCmd.Transaction = m_SqlTransaction;
  367. oraCmd.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  368. retValue = oraCmd.ExecuteScalar();
  369. }
  370. catch (Exception ex)
  371. {
  372. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  373. throw new Exception(strError, ex);
  374. }
  375. return retValue;
  376. }
  377. public byte[] GetSqlResultToBt(string p_strCommand, IDataParameter[] p_Parameter)
  378. {
  379. int bufferSize = 100;
  380. byte[] bytData = new byte[bufferSize];
  381. byte[] bytResult = null;
  382. int retval;
  383. int startIndex = 0;
  384. if (m_SqlConnection.State != ConnectionState.Open)
  385. Connect();
  386. try
  387. {
  388. //创建内存流,用于将byte[]数组写入到流中
  389. MemoryStream ms = new MemoryStream();
  390. SqlCommand sqlCommand = new SqlCommand(p_strCommand, m_SqlConnection);
  391. if (p_Parameter != null)
  392. {
  393. sqlCommand.Parameters.AddRange(p_Parameter);
  394. }
  395. sqlCommand.Transaction = m_SqlTransaction;
  396. SqlDataReader myReader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
  397. while (myReader.Read())
  398. {
  399. //读取blob列数据到bytData中
  400. retval = Convert.ToInt32(myReader.GetBytes(0, startIndex, bytData, 0, bufferSize));
  401. //将bytData写入到流中
  402. ms.Write(bytData, 0, retval);
  403. while (retval == bufferSize)
  404. {
  405. // 重新设置读取bytData数据后缓冲读取位置的开始索引
  406. startIndex += bufferSize;
  407. retval = Convert.ToInt32(myReader.GetBytes(0, startIndex, bytData, 0, bufferSize));
  408. ms.Write(bytData, 0, retval);
  409. }
  410. }
  411. bytResult = ms.ToArray();
  412. ms.Close();
  413. myReader.Close();
  414. }
  415. catch (Exception ex)
  416. {
  417. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  418. throw new Exception(strError, ex);
  419. }
  420. return bytResult;
  421. }
  422. /// <summary>
  423. /// 执行存储过程
  424. /// </summary>
  425. /// <param name="p_strCommand">存储过程名称</param>
  426. /// <param name="p_Parameter">IDataParameter类型 参数集合</param>
  427. /// <returns></returns>
  428. public DataSet ExecStoredProcedure(string p_strCommand, IDataParameter[] p_Parameter)
  429. {
  430. DataSet myDs = new DataSet();
  431. if (m_SqlConnection.State != ConnectionState.Open)
  432. Connect();
  433. try
  434. {
  435. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  436. if (p_Parameter != null)
  437. {
  438. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  439. }
  440. dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
  441. dataAdapter.SelectCommand.Transaction = m_SqlTransaction;
  442. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  443. dataAdapter.Fill(myDs);
  444. }
  445. catch (Exception ex)
  446. {
  447. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  448. throw new Exception(strError, ex);
  449. }
  450. return myDs;
  451. }
  452. #endregion
  453. }
  454. }