MSSQLServerDBTransaction.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 ConnectionState Close()
  150. {
  151. return Disconnect();
  152. }
  153. public void Commit()
  154. {
  155. try
  156. {
  157. m_SqlTransaction.Commit();
  158. }
  159. catch (Exception ex)
  160. {
  161. string strError = ex.Message + ex.Source;
  162. throw new Exception(strError, ex);
  163. }
  164. }
  165. public void Rollback()
  166. {
  167. try
  168. {
  169. m_SqlTransaction.Rollback();
  170. }
  171. catch (Exception ex)
  172. {
  173. string strError = ex.Message + ex.Source;
  174. throw new Exception(strError, ex);
  175. }
  176. }
  177. public void RollbackTo(string p_strcheckPoint)
  178. {
  179. //try
  180. //{
  181. // m_SqlTransaction.Rollback();
  182. //}
  183. //catch (Exception ex)
  184. //{
  185. // string strError = ex.Message + ex.Source;
  186. // throw new Exception(strError, ex);
  187. //}
  188. m_SqlTransaction.Rollback(p_strcheckPoint);
  189. }
  190. public void SavePoint(string p_strcheckPoint)
  191. {
  192. //throw new Exception("不支持此方法!");
  193. m_SqlTransaction.Save(p_strcheckPoint);
  194. }
  195. public int ExecuteNonQuery(string p_strCommand, IDataParameter[] p_Parameter)
  196. {
  197. return ExecuteNonQuery(p_strCommand, false, p_Parameter);
  198. }
  199. public int ExecuteNonQuery(string p_strCommand, bool p_procedure, IDataParameter[] p_Parameter)
  200. {
  201. int blReturn = 0;
  202. if (m_ConnState != ConnectionState.Open)
  203. Connect();
  204. try
  205. {
  206. SqlCommand myCommand = new SqlCommand(p_strCommand, m_SqlConnection);
  207. if (p_Parameter != null)
  208. {
  209. myCommand.Parameters.AddRange(p_Parameter);
  210. }
  211. myCommand.Transaction = m_SqlTransaction;
  212. if (p_procedure)
  213. {
  214. myCommand.CommandType = CommandType.StoredProcedure;
  215. }
  216. else
  217. {
  218. myCommand.CommandType = CommandType.Text;
  219. }
  220. blReturn = myCommand.ExecuteNonQuery();
  221. if (m_AutoCommit)
  222. Commit();
  223. }
  224. catch (Exception ex)
  225. {
  226. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  227. throw new Exception(strError, ex);
  228. }
  229. return blReturn;
  230. }
  231. public int UpdateBlob(string p_strTable, string p_strColumn, byte[] p_blobData, string p_strWhere)
  232. {
  233. int intReturn = 0;
  234. string strCommand = "UPDATE " + p_strTable + " SET " + p_strColumn + " =@blobValue where " + p_strWhere;
  235. if (m_SqlConnection.State != ConnectionState.Open)
  236. Connect();
  237. try
  238. {
  239. SqlCommand myCommand = new SqlCommand(strCommand, m_SqlConnection);
  240. myCommand.Transaction = m_SqlTransaction;
  241. SqlParameter aseParam = new SqlParameter("@blobValue", SqlDbType.Image);
  242. aseParam.Direction = ParameterDirection.InputOutput;
  243. aseParam.Value = p_blobData;
  244. myCommand.Parameters.Add(aseParam);
  245. intReturn = myCommand.ExecuteNonQuery();
  246. if (m_AutoCommit)
  247. Commit();
  248. intReturn = 1;
  249. }
  250. catch (Exception ex)
  251. {
  252. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + strCommand;
  253. throw new Exception(strError, ex);
  254. }
  255. return intReturn;
  256. }
  257. public DataSet GetSqlResultToDs(string p_strCommand, IDataParameter[] p_Parameter)
  258. {
  259. DataSet myDs = new DataSet();
  260. if (m_SqlConnection.State != ConnectionState.Open)
  261. Connect();
  262. try
  263. {
  264. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  265. if (p_Parameter != null)
  266. {
  267. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  268. }
  269. dataAdapter.SelectCommand.Transaction = m_SqlTransaction;
  270. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  271. dataAdapter.Fill(myDs);
  272. }
  273. catch (Exception ex)
  274. {
  275. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  276. throw new Exception(strError, ex);
  277. }
  278. return myDs;
  279. }
  280. public DataSet GetSqlResultToDs(string p_strCommand, string p_strName, IDataParameter[] p_Parameter)
  281. {
  282. DataSet myDs = new DataSet();
  283. if (m_SqlConnection.State != ConnectionState.Open)
  284. Connect();
  285. try
  286. {
  287. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  288. if (p_Parameter != null)
  289. {
  290. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  291. }
  292. dataAdapter.SelectCommand.Transaction = m_SqlTransaction;
  293. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  294. dataAdapter.Fill(myDs, p_strName);
  295. }
  296. catch (Exception ex)
  297. {
  298. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  299. throw new Exception(strError, ex);
  300. }
  301. return myDs;
  302. }
  303. public DataTable GetSqlResultToDt(string p_strCommand, IDataParameter[] p_Parameter)
  304. {
  305. //初始化结果集(DT)
  306. DataTable myDt = new DataTable();
  307. if (m_SqlConnection.State != ConnectionState.Open)
  308. Connect();
  309. try
  310. {
  311. //dataAdapte实例化
  312. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  313. if (p_Parameter != null)
  314. {
  315. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  316. }
  317. dataAdapter.SelectCommand.Transaction = m_SqlTransaction;
  318. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  319. //填充数据集(DT)
  320. dataAdapter.Fill(myDt);
  321. }
  322. catch (Exception ex)
  323. {
  324. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  325. throw new Exception(strError, ex);
  326. }
  327. //返回数据集
  328. return myDt;
  329. }
  330. public string GetSqlResultToStr(string p_strCommand, IDataParameter[] p_Parameter)
  331. {
  332. string strResult = "";
  333. object retValue = null;
  334. if (m_ConnState != ConnectionState.Open)
  335. Connect();
  336. try
  337. {
  338. SqlCommand oraCmd = new SqlCommand(p_strCommand, m_SqlConnection);
  339. if (p_Parameter != null)
  340. {
  341. oraCmd.Parameters.AddRange(p_Parameter);
  342. }
  343. oraCmd.Transaction = m_SqlTransaction;
  344. oraCmd.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  345. retValue = oraCmd.ExecuteScalar();
  346. if (retValue != null)
  347. strResult = retValue.ToString();
  348. else
  349. strResult = String.Empty;
  350. }
  351. catch (Exception ex)
  352. {
  353. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  354. throw new Exception(strError, ex);
  355. }
  356. return strResult;
  357. }
  358. public object GetSqlResultToObj(string p_strCommand, IDataParameter[] p_Parameter)
  359. {
  360. object retValue = null;
  361. if (m_ConnState != ConnectionState.Open)
  362. Connect();
  363. try
  364. {
  365. SqlCommand oraCmd = new SqlCommand(p_strCommand, m_SqlConnection);
  366. if (p_Parameter != null)
  367. {
  368. oraCmd.Parameters.AddRange(p_Parameter);
  369. }
  370. oraCmd.Transaction = m_SqlTransaction;
  371. oraCmd.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  372. retValue = oraCmd.ExecuteScalar();
  373. }
  374. catch (Exception ex)
  375. {
  376. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  377. throw new Exception(strError, ex);
  378. }
  379. return retValue;
  380. }
  381. public byte[] GetSqlResultToBt(string p_strCommand, IDataParameter[] p_Parameter)
  382. {
  383. int bufferSize = 100;
  384. byte[] bytData = new byte[bufferSize];
  385. byte[] bytResult = null;
  386. int retval;
  387. int startIndex = 0;
  388. if (m_SqlConnection.State != ConnectionState.Open)
  389. Connect();
  390. try
  391. {
  392. //创建内存流,用于将byte[]数组写入到流中
  393. MemoryStream ms = new MemoryStream();
  394. SqlCommand sqlCommand = new SqlCommand(p_strCommand, m_SqlConnection);
  395. if (p_Parameter != null)
  396. {
  397. sqlCommand.Parameters.AddRange(p_Parameter);
  398. }
  399. sqlCommand.Transaction = m_SqlTransaction;
  400. SqlDataReader myReader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
  401. while (myReader.Read())
  402. {
  403. //读取blob列数据到bytData中
  404. retval = Convert.ToInt32(myReader.GetBytes(0, startIndex, bytData, 0, bufferSize));
  405. //将bytData写入到流中
  406. ms.Write(bytData, 0, retval);
  407. while (retval == bufferSize)
  408. {
  409. // 重新设置读取bytData数据后缓冲读取位置的开始索引
  410. startIndex += bufferSize;
  411. retval = Convert.ToInt32(myReader.GetBytes(0, startIndex, bytData, 0, bufferSize));
  412. ms.Write(bytData, 0, retval);
  413. }
  414. }
  415. bytResult = ms.ToArray();
  416. ms.Close();
  417. myReader.Close();
  418. }
  419. catch (Exception ex)
  420. {
  421. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  422. throw new Exception(strError, ex);
  423. }
  424. return bytResult;
  425. }
  426. /// <summary>
  427. /// 执行存储过程
  428. /// </summary>
  429. /// <param name="p_strCommand">存储过程名称</param>
  430. /// <param name="p_Parameter">IDataParameter类型 参数集合</param>
  431. /// <returns></returns>
  432. public DataSet ExecStoredProcedure(string p_strCommand, IDataParameter[] p_Parameter)
  433. {
  434. DataSet myDs = new DataSet();
  435. if (m_SqlConnection.State != ConnectionState.Open)
  436. Connect();
  437. try
  438. {
  439. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  440. if (p_Parameter != null)
  441. {
  442. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  443. }
  444. dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
  445. dataAdapter.SelectCommand.Transaction = m_SqlTransaction;
  446. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  447. dataAdapter.Fill(myDs);
  448. }
  449. catch (Exception ex)
  450. {
  451. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  452. throw new Exception(strError, ex);
  453. }
  454. return myDs;
  455. }
  456. #endregion
  457. }
  458. }