当前位置:Gxlcms > 数据库问题 > Accesshelper.cs

Accesshelper.cs

时间:2021-07-01 10:21:17 帮助过:12人阅读

  1. using System;
  2. using System.Data;
  3. using System.Data.OleDb;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Globalization;
  7. using System.Configuration;
  8. namespace ArticleManage
  9. {
  10. public class DataAccess
  11. {
  12. 定义#region 定义
  13. protected OleDbCommand Comm;
  14. protected OleDbDataAdapter Adap;
  15. protected OleDbConnection Conn; //SQL连接
  16. private string _connectString; //连接串
  17. private string _commandString; //SQL命令
  18. private Hashtable _dict, _result, _mapTable;
  19. private DataSet _ds; //返回结果数据集
  20. private DataRow _recordSet; //纪录集
  21. private string _tableName; //表名
  22. private int _recordCount; //纪录集的行数
  23. private bool _eOF; //结果集是否为空,是否已经到了结尾
  24. private string DB;
  25. private string _deleteOP;
  26. private string _path;
  27. private StreamWriter SWCreate, SWApp;
  28. private string _errorMessage;
  29. private bool _writeLog;
  30. #endregion
  31. 属性集#region 属性集
  32. /**//// <summary>
  33. /// 出错信息
  34. /// </summary>
  35. ///
  36. public string ErrorMessage
  37. {
  38. get { return this._errorMessage; }
  39. set { this._errorMessage = value; }
  40. }
  41. /**//**/
  42. /**//// <summary>
  43. /// 设置或者取得删除的操作者
  44. /// </summary>
  45. public string DeleteOP
  46. {
  47. get { return this._deleteOP; }
  48. set { this._deleteOP = value; }
  49. }
  50. /**//**/
  51. /**//// <summary>
  52. /// 取得是否溢出
  53. /// </summary>
  54. public bool EOF
  55. {
  56. get { return this._eOF; }
  57. set { this._eOF = value; }
  58. }
  59. /**//**/
  60. /**//// <summary>
  61. /// 取得执行语句后得出的纪录条数
  62. /// </summary>
  63. public int RecordCount
  64. {
  65. get { return this._recordCount; }
  66. set { this._recordCount = value; }
  67. }
  68. /**//**/
  69. /**//// <summary>
  70. /// 数据库中的表名
  71. /// </summary>
  72. public string TableName
  73. {
  74. get { return this._tableName; }
  75. set { this._tableName = value; }
  76. }
  77. /**//**/
  78. /**//// <summary>
  79. /// 返回的记录集
  80. /// </summary>
  81. public DataRow RecordSet
  82. {
  83. get { return this._recordSet; }
  84. set { this._recordSet = value; }
  85. }
  86. /**//**/
  87. /**//// <summary>
  88. /// 返回的数据集
  89. /// </summary>
  90. public DataSet DS
  91. {
  92. get { return this._ds; }
  93. set { this._ds = value; }
  94. }
  95. /**//**/
  96. /**//// <summary>
  97. /// 字段和控件的映射表
  98. /// </summary>
  99. public Hashtable MapTable
  100. {
  101. get { return this._mapTable; }
  102. set { this._mapTable = value; }
  103. }
  104. /**//**/
  105. /**//// <summary>
  106. /// 修改数据时,作为修改结果
  107. /// </summary>
  108. public Hashtable Result
  109. {
  110. get { return this._result; }
  111. set { this._result = value; }
  112. }
  113. /**//**/
  114. /**//// <summary>
  115. /// 保存数据用的字段和值对应的哈希表,修改数据时用作条件
  116. /// </summary>
  117. public Hashtable Dict
  118. {
  119. get { return this._dict; }
  120. set { this._dict = value; }
  121. }
  122. /**//**/
  123. /**//// <summary>
  124. /// 查询语句
  125. /// </summary>
  126. public string CommandString
  127. {
  128. get { return this._commandString; }
  129. set { this._commandString = value; }
  130. }
  131. /**//**/
  132. /**//// <summary>
  133. /// 连接串
  134. /// </summary>
  135. public string ConnectString
  136. {
  137. get { return this._connectString; }
  138. set { this._connectString = value; }
  139. }
  140. #endregion
  141. DataAccess的构造函数#region DataAccess的构造函数
  142. /**//// <summary>
  143. /// 空构造函数
  144. /// <appSettings>
  145. /// <add key="DBConn" value="provider=microsoft.jet.oledb.4.0;data source="/>
  146. /// <add key="dbPath" value="~/App_Data/ArticleManage.mdb"/>
  147. ///</appSettings>
  148. /// </summary>
  149. public DataAccess()
  150. {
  151. ConnectString = System.Configuration.ConfigurationSettings.AppSettings["DBConn"]+System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["dbPath"])+";";
  152. Conn = new System.Data.OleDb.OleDbConnection(ConnectString);
  153. if (System.Configuration.ConfigurationSettings.AppSettings["WriteLog"] == "true")
  154. {
  155. _writeLog = true;
  156. }
  157. else
  158. {
  159. _writeLog = false;
  160. }
  161. }
  162. ~DataAccess()
  163. {
  164. }
  165. /**//**/
  166. /**//// <summary>
  167. /// DataAccess的构造函数
  168. /// <appSettings>
  169. /// <add key="DBConn" value="provider=microsoft.jet.oledb.4.0;data source="/>
  170. /// <add key="DB1" value="~/App_Data/ArticleManage.mdb"/>
  171. ///</appSettings>
  172. /// </summary>
  173. /// <param name="DB1">要访问的数据库名,Web.config里设置的连接字符串对应的key</param>
  174. /// <param name="TableName1">要访问的数据表名</param>
  175. public DataAccess(string DB1, string TableName1)
  176. {
  177. this.ErrorMessage = "";
  178. DB = DB1;
  179. TableName = TableName1;
  180. try
  181. {
  182. ConnectString = System.Configuration.ConfigurationSettings.AppSettings["DBConn"]+System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["DB"])+";";
  183. if (System.Configuration.ConfigurationSettings.AppSettings["WriteLog"] == "true")
  184. {
  185. _writeLog = true;
  186. }
  187. else
  188. {
  189. _writeLog = false;
  190. }
  191. Conn = new System.Data.OleDb.OleDbConnection(ConnectString);
  192. Dict = new Hashtable();
  193. Result = new Hashtable();
  194. MapTable = new Hashtable();
  195. DS = new DataSet();
  196. // IS_Open = false;
  197. _path = "C:\\WebDebug.log";
  198. if (_writeLog)
  199. {
  200. if (!File.Exists(_path))
  201. {
  202. using (SWCreate = File.CreateText(_path))
  203. {
  204. SWCreate.WriteLine(" ");
  205. SWCreate.Close();
  206. }
  207. }
  208. using (SWApp = File.AppendText(_path))
  209. {
  210. SWApp.WriteLine(" ");
  211. }
  212. }
  213. }
  214. catch (Exception e)
  215. {
  216. this.ErrorMessage = e.ToString();
  217. }
  218. }
  219. /**//**/
  220. /**//// <summary>
  221. /// DataAccess的构造函数
  222. /// </summary>
  223. /// <param name="CST">数据库的连接字符串</param>
  224. /// <param name="TableName1">要访问的数据表名</param>
  225. /// <param name="flag">是否初始化</param>
  226. public DataAccess(string CST, string TableName1, bool flag)
  227. {
  228. if (flag == true)
  229. {
  230. this.ErrorMessage = "";
  231. TableName = TableName1;
  232. try
  233. {
  234. if (System.Configuration.ConfigurationSettings.AppSettings["WriteLog"] == "true")
  235. {
  236. _writeLog = true;
  237. }
  238. else
  239. {
  240. _writeLog = false;
  241. }
  242. ConnectString = CST;
  243. Conn = new System.Data.OleDb.OleDbConnection(ConnectString);
  244. Dict = new Hashtable();
  245. Result = new Hashtable();
  246. MapTable = new Hashtable();
  247. DS = new DataSet();
  248. _path = "C:\\WebDebug.log";
  249. if (_writeLog)
  250. {
  251. if (!File.Exists(_path))
  252. {
  253. using (SWCreate = File.CreateText(_path))
  254. {
  255. SWCreate.WriteLine(" ");
  256. SWCreate.Close();
  257. }
  258. }
  259. using (SWApp = File.AppendText(_path))
  260. {
  261. SWApp.WriteLine(" ");
  262. }
  263. }
  264. }
  265. catch (Exception e)
  266. {
  267. this.ErrorMessage = e.ToString();
  268. }
  269. }
  270. }
  271. #endregion
  272. ExecuteNonQuery#region ExecuteNonQuery
  273. /**//// <summary>
  274. /// 执行无返回结果的SQL
  275. /// </summary>
  276. /// <param name="strSQL"></param>
  277. public void ExecuteNonQuery(string strSQL)
  278. {
  279. Comm = new OleDbCommand();
  280. OleDbTransaction Trans;
  281. Conn.Open();
  282. Trans = Conn.BeginTransaction();
  283. Comm.CommandText = strSQL;
  284. Comm.Connection = Conn;
  285. Comm.Transaction = Trans;
  286. Comm.CommandTimeout = 60;
  287. try
  288. {
  289. if (_writeLog)
  290. {
  291. using (SWApp = File.AppendText(_path))
  292. {
  293. SWApp.WriteLine("[" + DateTime.Now.ToString() + "] CommandString = " + strSQL);
  294. SWApp.Close();
  295. }
  296. }
  297. Comm.ExecuteNonQuery();
  298. Trans.Commit();
  299. this.Conn.Close();
  300. }
  301. catch (Exception e)
  302. {
  303. Trans.Rollback();
  304. this.Conn.Close();
  305. if (_writeLog)
  306. {
  307. using (SWApp = File.AppendText(_path))
  308. {
  309. SWApp.WriteLine("[" + DateTime.Now.ToString() + "] Error Message : " + e.ToString());
  310. }
  311. }
  312. this.ErrorMessage = e.ToString();
  313. throw new Exception(e.ToString());
  314. }
  315. }
  316. /**//// <summary>
  317. /// 执行无返回结果的SQL
  318. /// </summary>
  319. /// <param name="param">参数集合</param>
  320. /// <param name="strSQL"></param>
  321. public void ExecuteNonQuery(string strSQL, ICollection param)
  322. {
  323. Comm = new OleDbCommand();
  324. OleDbTransaction Trans;
  325. Conn.Open();
  326. Trans = Conn.BeginTransaction();
  327. Comm.CommandText = strSQL;
  328. Comm.Connection = Conn;
  329. Comm.Transaction = Trans;
  330. Comm.CommandTimeout = 60;
  331. try
  332. {
  333. if (_writeLog)
  334. {
  335. using (SWApp = File.AppendText(_path))
  336. {
  337. SWApp.WriteLine("[" + DateTime.Now.ToString() + "] CommandString = " + strSQL);
  338. SWApp.Close();
  339. }
  340. }
  341. if (param != null)
  342. {
  343. foreach (ParamInfo p in param)
  344. {
  345. IDbDataParameter pa = Comm.CreateParameter();
  346. pa.ParameterName = p.Name;
  347. pa.Value = p.Value;
  348. //处理大文本
  349. if (pa is System.Data.OleDb.OleDbParameter && pa.Value != null && pa.Value.ToString().Length >= 4000)
  350. {
  351. System.Data.OleDb.OleDbParameter p1 = pa as System.Data.OleDb.OleDbParameter;
  352. p1.OleDbType = System.Data.OleDb.OleDbType.VarWChar;
  353. Comm.Parameters.Add(p1);
  354. }
  355. else
  356. {
  357. Comm.Parameters.Add(pa);
  358. }
  359. }
  360. }
  361. Comm.ExecuteNonQuery();
  362. FillParameterValue(Comm.Parameters, param);
  363. Trans.Commit();
  364. this.Conn.Close();
  365. }
  366. catch (Exception e)
  367. {
  368. Trans.Rollback();
  369. this.Conn.Close();
  370. if (_writeLog)
  371. {
  372. using (SWApp = File.AppendText(_path))
  373. {
  374. SWApp.WriteLine("[" + DateTime.Now.ToString() + "] Error Message : " + e.ToString());
  375. }
  376. }
  377. this.ErrorMessage = e.ToString();
  378. throw new Exception(e.ToString());
  379. }
  380. }
  381. #endregion
  382. ExecuteScalar#region ExecuteScalar
  383. /**//// <summary>
  384. /// 返回查询结果的第一行第一列的值
  385. /// </summary>
  386. /// <param name="strSQL"></param>
  387. /// <returns></returns>
  388. public object ExecuteScalar(string strSQL)
  389. {
  390. OleDbTransaction Trans;
  391. Comm = new OleDbCommand();
  392. Conn.Open();
  393. Trans = Conn.BeginTransaction();
  394. Comm.CommandText = strSQL;
  395. Comm.Connection = Conn;
  396. Comm.Transaction =Trans ;
  397. Comm.CommandTimeout = 60;
  398. try
  399. {
  400. if (_writeLog)
  401. {
  402. using (SWApp = File.AppendText(_path))
  403. {
  404. SWApp.WriteLine("[" + DateTime.Now.ToString() + "] CommandString = " + strSQL);
  405. SWApp.Close();
  406. }
  407. }
  408. object objResutl = Comm.ExecuteScalar();
  409. Trans.Commit();
  410. this.Conn.Close();
  411. return objResutl;
  412. }
  413. catch (Exception e)
  414. {
  415. Trans.Rollback();
  416. this.Conn.Close();
  417. if (_writeLog)
  418. {
  419. using (SWApp = File.AppendText(_path))
  420. {
  421. SWApp.WriteLine("[" + DateTime.Now.ToString() + "] Error Message : " + e.ToString());
  422. }
  423. }
  424. this.ErrorMessage = e.ToString();
  425. throw new Exception(e.ToString());
  426. }
  427. }
  428. /**//// <summary>
  429. /// 返回查询结果的第一行第一列的值
  430. /// </summary>
  431. /// <param name="strSQL"></param>
  432. /// <param name="param">参数集合</param>
  433. /// <returns></returns>
  434. public object ExecuteScalar(string strSQL,ICollection param)
  435. {
  436. OleDbTransaction Trans;
  437. Comm = new OleDbCommand();
  438. Conn.Open();
  439. Trans = Conn.BeginTransaction();
  440. Comm.CommandText = strSQL;
  441. Comm.Connection = Conn;
  442. Comm.Transaction =Trans ;
  443. Comm.CommandTimeout = 60;
  444. try
  445. {
  446. if (_writeLog)
  447. {
  448. using (SWApp = File.AppendText(_path))
  449. {
  450. SWApp.WriteLine("[" + DateTime.Now.ToString() + "] CommandString = " + strSQL);
  451. SWApp.Close();
  452. }
  453. }
  454. if ( param != null )
  455. {
  456. foreach ( ParamInfo p in param )
  457. {
  458. IDbDataParameter pa = Comm.CreateParameter();
  459. pa.ParameterName = p.Name;
  460. pa.Value = p.Value;
  461. pa.Direction = p.Direction;
  462. Comm.Parameters.Add(pa);
  463. }
  464. }
  465. object objResutl = Comm.ExecuteScalar();
  466. FillParameterValue(Comm.Parameters,param);
  467. Trans.Commit();
  468. this.Conn.Close();
  469. return objResutl;
  470. }
  471. catch (Exception e)
  472. {
  473. Trans.Rollback();
  474. this.Conn.Close();
  475. if (_writeLog)
  476. {
  477. using (SWApp = File.AppendText(_path))
  478. {
  479. SWApp.WriteLine("[" + DateTime.Now.ToString() + "] Error Message : " + e.ToString());
  480. }
  481. }
  482. this.ErrorMessage = e.ToString();
  483. throw new Exception(e.ToString());
  484. }
  485. }
  486. #endregion
  487. ExecuteDataSet#region ExecuteDataSet
  488. /**//// <summary>
  489. /// 执行SQL语句并返回DataTable对象
  490. /// </summary>
  491. public DataSet ExecuteDataSet(string strSQL)
  492. {
  493. OleDbTransaction Trans;
  494. Comm = new OleDbCommand();
  495. Conn.Open();
  496. Trans = Conn.BeginTransaction();
  497. Comm.CommandText = strSQL;
  498. Comm.Connection = Conn;
  499. Comm.Transaction =Trans ;
  500. Comm.CommandTimeout = 60;
  501. DataSet ds = new DataSet();
  502. try
  503. {
  504. if (_writeLog)
  505. {
  506. using (SWApp = File.AppendText(_path))
  507. {
  508. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> Begin Get DataSet.");
  509. SWApp.WriteLine("CommandString = " + strSQL);
  510. SWApp.Close();
  511. }
  512. }
  513. try
  514. {
  515. IDataReader dr = Comm.ExecuteReader();
  516. do
  517. {
  518. DataTable dt = new DataTable();
  519. dt.Locale = CultureInfo.CurrentCulture;
  520. DataColumn col = null;
  521. DataRowCollection rows = dr.GetSchemaTable().Rows;
  522. foreach(DataRow row in rows)
  523. {
  524. col = new DataColumn();
  525. col.ColumnName = row["ColumnName"] == null ? null : row["ColumnName"].ToString();
  526. col.Unique = Convert.ToBoolean(row["IsUnique"]);
  527. col.AllowDBNull = Convert.ToBoolean(row["AllowDBNull"]);
  528. col.ReadOnly = Convert.ToBoolean(row["IsReadOnly"]);
  529. col.DataType = row["DataType"] as Type;
  530. dt.Columns.Add(col);
  531. }
  532. while (dr.Read())
  533. {
  534. DataRow row = dt.NewRow();
  535. foreach(DataColumn c in dt.Columns)
  536. {
  537. row[c] = dr[c.ColumnName];
  538. }
  539. dt.Rows.Add(row);
  540. }
  541. ds.Tables.Add(dt);
  542. }
  543. while (dr.NextResult());
  544. dr.Close();
  545. Trans.Commit();
  546. }
  547. catch (Exception e)
  548. {
  549. Trans.Rollback();
  550. this.ErrorMessage = e.ToString();
  551. if (_writeLog)
  552. {
  553. using (SWApp = File.AppendText(_path))
  554. {
  555. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> Error Message: " + e.ToString());
  556. }
  557. }
  558. throw new Exception(e.ToString());
  559. }
  560. if (_writeLog)
  561. {
  562. using (SWApp = File.AppendText(_path))
  563. {
  564. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> End of Getting DataSet.");
  565. SWApp.Close();
  566. }
  567. }
  568. }
  569. finally
  570. {
  571. Conn.Close();
  572. }
  573. return ds;
  574. }
  575. /**//// <summary>
  576. /// 执行SQL语句并返回DataTable对象
  577. /// <param name="strSQL">SQL语句</param>
  578. /// <param name="param">参数集合</param>
  579. /// </summary>
  580. public DataSet ExecuteDataSet(string strSQL, ICollection param)
  581. {
  582. OleDbTransaction Trans;
  583. Comm = new OleDbCommand();
  584. Conn.Open();
  585. Trans = Conn.BeginTransaction();
  586. Comm.CommandText = strSQL;
  587. Comm.Connection = Conn;
  588. Comm.Transaction =Trans ;
  589. Comm.CommandTimeout = 60;
  590. DataSet ds = new DataSet();
  591. try
  592. {
  593. if (_writeLog)
  594. {
  595. using (SWApp = File.AppendText(_path))
  596. {
  597. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> Begin Get DataSet.");
  598. SWApp.WriteLine("CommandString = " + strSQL);
  599. SWApp.Close();
  600. }
  601. }
  602. try
  603. {
  604. if ( param != null )
  605. {
  606. foreach ( ParamInfo p in param )
  607. {
  608. IDbDataParameter pa = Comm.CreateParameter();
  609. pa.ParameterName = p.Name;
  610. pa.Value = p.Value;
  611. pa.Direction = p.Direction;
  612. Comm.Parameters.Add(pa);
  613. }
  614. }
  615. IDataReader dr = Comm.ExecuteReader();
  616. do
  617. {
  618. DataTable dt = new DataTable();
  619. dt.Locale = CultureInfo.CurrentCulture;
  620. DataColumn col = null;
  621. DataRowCollection rows = dr.GetSchemaTable().Rows;
  622. foreach(DataRow row in rows)
  623. {
  624. col = new DataColumn();
  625. col.ColumnName = row["ColumnName"] == null ? null : row["ColumnName"].ToString();
  626. col.Unique = Convert.ToBoolean(row["IsUnique"]);
  627. col.AllowDBNull = Convert.ToBoolean(row["AllowDBNull"]);
  628. col.ReadOnly = Convert.ToBoolean(row["IsReadOnly"]);
  629. col.DataType = row["DataType"] as Type;
  630. dt.Columns.Add(col);
  631. }
  632. while (dr.Read())
  633. {
  634. DataRow row = dt.NewRow();
  635. foreach(DataColumn c in dt.Columns)
  636. {
  637. row[c] = dr[c.ColumnName];
  638. }
  639. dt.Rows.Add(row);
  640. }
  641. ds.Tables.Add(dt);
  642. }
  643. while (dr.NextResult());
  644. dr.Close();
  645. Trans.Commit();
  646. }
  647. catch (Exception e)
  648. {
  649. Trans.Rollback();
  650. this.ErrorMessage = e.ToString();
  651. if (_writeLog)
  652. {
  653. using (SWApp = File.AppendText(_path))
  654. {
  655. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> Error Message: " + e.ToString());
  656. }
  657. }
  658. throw new Exception(e.ToString());
  659. }
  660. if (_writeLog)
  661. {
  662. using (SWApp = File.AppendText(_path))
  663. {
  664. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> End of Getting DataSet.");
  665. SWApp.Close();
  666. }
  667. }
  668. }
  669. finally
  670. {
  671. Conn.Close();
  672. }
  673. return ds;
  674. }
  675. #endregion
  676. ExecuteDataTable#region ExecuteDataTable
  677. /**//// <summary>
  678. /// 执行SQL语句并返回DataTable对象
  679. /// </summary>
  680. public DataTable ExecuteDataTable(string strSQL)
  681. {
  682. return ExecuteDataSet(strSQL).Tables[0];
  683. }
  684. /**//// <summary>
  685. /// 执行SQL语句并返回DataTable对象
  686. /// <param name="strSQL">SQL语句</param>
  687. /// <param name="param">参数集合</param>
  688. /// </summary>
  689. public DataTable ExecuteDataTable(string strSQL, ICollection param)
  690. {
  691. return ExecuteDataSet(strSQL,param).Tables[0];
  692. }
  693. #endregion
  694. ExecuteDataReader#region ExecuteDataReader
  695. /**//// <summary>
  696. /// <param name="strSQL">SQL语句</param>
  697. /// </summary>
  698. public IDataReader ExecuteDataReader(string strSQL)
  699. {
  700. OleDbTransaction Trans;
  701. Comm = new OleDbCommand();
  702. Conn.Open();
  703. Trans = Conn.BeginTransaction();
  704. Comm.CommandText = strSQL;
  705. Comm.Connection = Conn;
  706. Comm.Transaction = Trans;
  707. Comm.CommandTimeout = 60;
  708. IDataReader dr ;
  709. try
  710. {
  711. if (_writeLog)
  712. {
  713. using (SWApp = File.AppendText(_path))
  714. {
  715. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> Begin Get DataReader.");
  716. SWApp.WriteLine("CommandString = " + strSQL);
  717. SWApp.Close();
  718. }
  719. }
  720. try
  721. {
  722. dr=Comm.ExecuteReader();
  723. Trans.Commit();
  724. }
  725. catch (Exception e)
  726. {
  727. Trans.Rollback();
  728. this.ErrorMessage = e.ToString();
  729. if (_writeLog)
  730. {
  731. using (SWApp = File.AppendText(_path))
  732. {
  733. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> Error Message: " + e.ToString());
  734. }
  735. }
  736. throw new Exception(e.ToString());
  737. }
  738. if (_writeLog)
  739. {
  740. using (SWApp = File.AppendText(_path))
  741. {
  742. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> End of Getting DataReader.");
  743. SWApp.Close();
  744. }
  745. }
  746. }
  747. finally
  748. {
  749. Conn.Close();
  750. }
  751. return dr;
  752. }
  753. /**//// <summary>
  754. ///
  755. /// <param name="strSQL">SQL语句</param>
  756. /// <param name="param">参数集合</param>
  757. /// </summary>
  758. public IDataReader ExecuteDataReader(string strSQL, ICollection param)
  759. {
  760. OleDbTransaction Trans;
  761. Comm = new OleDbCommand();
  762. Conn.Open();
  763. Trans = Conn.BeginTransaction();
  764. Comm.CommandText = strSQL;
  765. Comm.Connection = Conn;
  766. Comm.Transaction = Trans;
  767. Comm.CommandTimeout = 60;
  768. IDataReader dr;
  769. try
  770. {
  771. if (_writeLog)
  772. {
  773. using (SWApp = File.AppendText(_path))
  774. {
  775. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> Begin Get DataReader.");
  776. SWApp.WriteLine("CommandString = " + strSQL);
  777. SWApp.Close();
  778. }
  779. }
  780. try
  781. {
  782. if (param != null)
  783. {
  784. foreach (ParamInfo p in param)
  785. {
  786. IDbDataParameter pa = Comm.CreateParameter();
  787. pa.ParameterName = p.Name;
  788. pa.Value = p.Value;
  789. pa.Direction = p.Direction;
  790. Comm.Parameters.Add(pa);
  791. }
  792. }
  793. dr = Comm.ExecuteReader();
  794. Trans.Commit();
  795. }
  796. catch (Exception e)
  797. {
  798. Trans.Rollback();
  799. this.ErrorMessage = e.ToString();
  800. if (_writeLog)
  801. {
  802. using (SWApp = File.AppendText(_path))
  803. {
  804. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> Error Message: " + e.ToString());
  805. }
  806. }
  807. throw new Exception(e.ToString());
  808. }
  809. if (_writeLog)
  810. {
  811. using (SWApp = File.AppendText(_path))
  812. {
  813. SWApp.WriteLine("[" + DateTime.Now.ToString() + "]==> End of Getting DataReader.");
  814. SWApp.Close();
  815. }
  816. }
  817. }
  818. finally
  819. {
  820. Conn.Close();
  821. }
  822. return dr;
  823. }
  824. #endregion
  825. FillParameterValue#region FillParameterValue
  826. /**//// <summary>
  827. /// 填充输出型参数和返回值型参数
  828. /// </summary>
  829. /// <param name="OutPutParameters">SQL命令执行后的参数集合</param>
  830. /// <param name="param">SQL命令执行前的参数集合</param>
  831. void FillParameterValue(System.Data.IDataParameterCollection OutPutParameters, ICollection param)
  832. {
  833. if (OutPutParameters == null || param == null) return;
  834. ArrayList procParam = new ArrayList();
  835. foreach (IDbDataParameter OleDbParameter in OutPutParameters)
  836. {
  837. foreach (ParamInfo p in param)
  838. {
  839. if (p.Name == OleDbParameter.ParameterName)
  840. {
  841. procParam.Add(new ParamInfo(p.Name, OleDbParameter.Value, p.Direction, p.Size));
  842. }
  843. }
  844. }
  845. ArrayList procOutParam = param as ArrayList;
  846. procOutParam.Clear();
  847. foreach (ParamInfo p in procParam) //填充参数值
  848. {
  849. procOutParam.Add(new ParamInfo(p.Name, p.Value, p.Direction, p.Size));
  850. }
  851. }
  852. #endregion
  853. }
  854. ParamInfo#region ParamInfo
  855. /**//// <summary>
  856. /// SQL参数结构体
  857. /// </summary>
  858. public struct ParamInfo
  859. {
  860. /**//// <summary>
  861. /// 参数名称
  862. /// </summary>
  863. public string Name;
  864. /**//// <summary>
  865. /// 值
  866. /// </summary>
  867. public object Value;
  868. /**//// <summary>
  869. /// 参数长度
  870. /// </summary>
  871. public int Size;
  872. /**//// <summary>
  873. /// 参数方向
  874. /// </summary>
  875. public ParameterDirection Direction;
  876. /**//// <summary>
  877. /// 初始化参数对象
  878. /// </summary>
  879. /// <param name="name">参数名称</param>
  880. /// <param name="val">值</param>
  881. public ParamInfo(string name, object val)
  882. {
  883. Name = name;
  884. Value = val;
  885. Direction = ParameterDirection.Input;
  886. Size = Value == null ? 50 : Value.ToString().Length;
  887. }
  888. /**//// <summary>
  889. /// 初始化参数对象
  890. /// </summary>
  891. /// <param name="name">参数名称</param>
  892. /// <param name="val">值</param>
  893. /// <param name="direction"></param>
  894. public ParamInfo(string name, object val, ParameterDirection direction)
  895. {
  896. Name = name;
  897. Value = val;
  898. Direction = direction;
  899. Size = Value == null ? 50 : Value.ToString().Length;
  900. }
  901. public ParamInfo(string name, object val, ParameterDirection direction, int size)
  902. {
  903. Name = name;
  904. Value = val;
  905. Direction = direction;
  906. Size = size;
  907. }
  908. }
  909. #endregion
  910. }

  

Accesshelper.cs

标签:

人气教程排行