MSSQLServerDBConnection.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:MSSQLServerDBConnection.cs
  5. * 2.功能描述:SQL数据库不带有事物的操作类
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 张国印 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Data.SqlClient;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. namespace Dongke.IBOSS.PRD.Basics.DataAccess
  19. {
  20. /// <summary>
  21. /// SQL数据库不带有事物的操作类
  22. /// </summary>
  23. public class MSSQLServerDBConnection : IDBConnection
  24. {
  25. private static ArrayList m_Pool = new ArrayList();
  26. private SqlConnection m_SqlConnection;
  27. private ConnectionState m_ConState = ConnectionState.Closed;
  28. private string m_Constr;
  29. /// <summary>
  30. /// 忽略大小写
  31. /// </summary>
  32. public bool IgnoreCase
  33. {
  34. get;
  35. set;
  36. }
  37. /// <summary>
  38. /// SQL执行限定超时
  39. /// </summary>
  40. public bool IsCommandTimeout
  41. {
  42. get;
  43. set;
  44. }
  45. public MSSQLServerDBConnection(string m_strConn)
  46. {
  47. try
  48. {
  49. m_Constr = m_strConn;
  50. }
  51. catch (Exception ex)
  52. {
  53. string strError = ex.Message + ex.Source;
  54. throw new Exception(strError, ex);
  55. }
  56. }
  57. #region IDBConnection 成员
  58. public int PoolSize
  59. {
  60. get
  61. {
  62. return m_Pool.Count;
  63. }
  64. }
  65. public ConnectionState ConnState
  66. {
  67. get
  68. {
  69. return m_ConState;
  70. }
  71. }
  72. public IDbConnection DbConnection
  73. {
  74. get
  75. {
  76. return m_SqlConnection;
  77. }
  78. set
  79. {
  80. m_SqlConnection = (value as SqlConnection);
  81. }
  82. }
  83. public string ConnStr
  84. {
  85. get { return m_Constr; }
  86. }
  87. public ConnectionState Open()
  88. {
  89. try
  90. {
  91. m_SqlConnection = new SqlConnection(m_Constr);
  92. m_SqlConnection.Open();
  93. m_ConState = m_SqlConnection.State;
  94. //m_Pool.Add(m_SqlConnection);
  95. }
  96. catch (Exception ex)
  97. {
  98. m_ConState = ConnectionState.Closed;
  99. string strError = ex.Message + ex.Source;
  100. throw new Exception(strError, ex);
  101. }
  102. return m_ConState;
  103. }
  104. public ConnectionState Close()
  105. {
  106. try
  107. {
  108. if (m_SqlConnection.State == ConnectionState.Open)
  109. {
  110. m_SqlConnection.Close();
  111. m_ConState = m_SqlConnection.State;
  112. }
  113. //if (m_Pool.Contains(m_SqlConnection))
  114. //{
  115. // m_Pool.Remove(m_SqlConnection);
  116. //}
  117. }
  118. catch (Exception ex)
  119. {
  120. m_ConState = ConnectionState.Closed;
  121. string strError = ex.Message + ex.Source;
  122. throw new Exception(strError, ex);
  123. }
  124. return m_ConState;
  125. }
  126. public DataSet GetSqlResultToDs(string p_strCommand, IDataParameter[] p_Parameter)
  127. {
  128. DataSet myDs = new DataSet();
  129. if (m_SqlConnection.State != ConnectionState.Open)
  130. Open();
  131. try
  132. {
  133. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  134. if (p_Parameter != null)
  135. {
  136. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  137. }
  138. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  139. dataAdapter.Fill(myDs);
  140. }
  141. catch (Exception ex)
  142. {
  143. ex.Source = ex.Source + " 对应的SQL语句为:" + p_strCommand;
  144. string strError = ex.Message + ex.Source;
  145. throw new Exception(strError, ex);
  146. }
  147. return myDs;
  148. }
  149. public DataSet GetSqlResultToDs(string p_strCommand, string p_strName, IDataParameter[] p_Parameter)
  150. {
  151. DataSet myDs = new DataSet();
  152. if (m_SqlConnection.State != ConnectionState.Open)
  153. Open();
  154. try
  155. {
  156. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  157. if (p_Parameter != null)
  158. {
  159. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  160. }
  161. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  162. dataAdapter.Fill(myDs, p_strName);
  163. }
  164. catch (Exception ex)
  165. {
  166. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  167. throw new Exception(strError, ex);
  168. }
  169. return myDs;
  170. }
  171. public DataTable GetSqlResultToDt(string p_strCommand, IDataParameter[] p_Parameter)
  172. {
  173. //初始化结果集(DT)
  174. DataTable myDt = new DataTable();
  175. if (m_SqlConnection.State != ConnectionState.Open)
  176. Open();
  177. try
  178. {
  179. //dataAdapte实例化
  180. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  181. if (p_Parameter != null)
  182. {
  183. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  184. }
  185. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  186. //填充数据集(DT)
  187. dataAdapter.Fill(myDt);
  188. }
  189. catch (Exception ex)
  190. {
  191. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  192. throw new Exception(strError, ex);
  193. }
  194. //返回数据集
  195. return myDt;
  196. }
  197. public string GetSqlResultToStr(string p_strCommand, IDataParameter[] p_Parameter)
  198. {
  199. string strResult = "";
  200. object retValue = null;
  201. if (m_ConState != ConnectionState.Open)
  202. Open();
  203. try
  204. {
  205. SqlCommand oraCmd = new SqlCommand(p_strCommand, m_SqlConnection);
  206. if (p_Parameter != null)
  207. {
  208. oraCmd.Parameters.AddRange(p_Parameter);
  209. }
  210. oraCmd.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  211. retValue = oraCmd.ExecuteScalar();
  212. if (retValue != null)
  213. strResult = retValue.ToString();
  214. else
  215. strResult = String.Empty;
  216. }
  217. catch (Exception ex)
  218. {
  219. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  220. throw new Exception(strError, ex);
  221. }
  222. return strResult;
  223. }
  224. public object GetSqlResultToObj(string p_strCommand, IDataParameter[] p_Parameter)
  225. {
  226. object retValue = null;
  227. if (m_ConState != ConnectionState.Open)
  228. Open();
  229. try
  230. {
  231. SqlCommand oraCmd = new SqlCommand(p_strCommand, m_SqlConnection);
  232. if (p_Parameter != null)
  233. {
  234. oraCmd.Parameters.AddRange(p_Parameter);
  235. }
  236. oraCmd.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  237. retValue = oraCmd.ExecuteScalar();
  238. }
  239. catch (Exception ex)
  240. {
  241. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  242. throw new Exception(strError, ex);
  243. }
  244. return retValue;
  245. }
  246. public byte[] GetSqlResultToBt(string p_strCommand, IDataParameter[] p_Parameter)
  247. {
  248. int bufferSize = 100;
  249. byte[] bytData = new byte[bufferSize];
  250. byte[] bytResult = null;
  251. int retval;
  252. int startIndex = 0;
  253. if (m_SqlConnection.State != ConnectionState.Open)
  254. Open();
  255. try
  256. {
  257. //创建内存流,用于将byte[]数组写入到流中
  258. MemoryStream ms = new MemoryStream();
  259. SqlCommand sqlCommand = new SqlCommand(p_strCommand, m_SqlConnection);
  260. if (p_Parameter != null)
  261. {
  262. sqlCommand.Parameters.AddRange(p_Parameter);
  263. }
  264. SqlDataReader myReader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
  265. while (myReader.Read())
  266. {
  267. //读取blob列数据到bytData中
  268. retval = Convert.ToInt32(myReader.GetBytes(0, startIndex, bytData, 0, bufferSize));
  269. //将bytData写入到流中
  270. ms.Write(bytData, 0, retval);
  271. while (retval == bufferSize)
  272. {
  273. // 重新设置读取bytData数据后缓冲读取位置的开始索引
  274. startIndex += bufferSize;
  275. retval = Convert.ToInt32(myReader.GetBytes(0, startIndex, bytData, 0, bufferSize));
  276. ms.Write(bytData, 0, retval);
  277. }
  278. }
  279. bytResult = ms.ToArray();
  280. ms.Close();
  281. myReader.Close();
  282. }
  283. catch (Exception ex)
  284. {
  285. string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
  286. throw new Exception(strError, ex);
  287. }
  288. return bytResult;
  289. }
  290. /// <summary>
  291. /// 执行存储过程
  292. /// </summary>
  293. /// <param name="p_strCommand">存储过程名称</param>
  294. /// <param name="p_Parameter">IDataParameter类型 参数集合</param>
  295. /// <returns></returns>
  296. public DataSet ExecStoredProcedure(string p_strCommand, IDataParameter[] p_Parameter)
  297. {
  298. DataSet myDs = new DataSet();
  299. if (m_SqlConnection.State != ConnectionState.Open)
  300. Open();
  301. try
  302. {
  303. SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
  304. if (p_Parameter != null)
  305. {
  306. dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
  307. }
  308. dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
  309. dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
  310. dataAdapter.Fill(myDs);
  311. }
  312. catch (Exception ex)
  313. {
  314. ex.Source = ex.Source + " 对应的SQL语句为:" + p_strCommand;
  315. string strError = ex.Message + ex.Source;
  316. throw new Exception(strError, ex);
  317. }
  318. return myDs;
  319. }
  320. #endregion
  321. }
  322. }