1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using System.Runtime.Remoting.Messaging;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Configuration;
  8. namespace LTP.SQLServerDAL
  9. {
  10. /// <summary>
  11. /// ADO.NET数据库操作基础类。
  12. /// </summary>
  13. public abstract class DbManagerSQL
  14. {
  15. //数据库连接字符串
  16. protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
  17. public DbManagerSQL()
  18. {
  19. //
  20. // TODO: 在此处添加构造函数逻辑
  21. //
  22. }
  23. /// <summary>
  24. /// 执行SQL语句,返回影响的记录数
  25. /// </summary>
  26. /// <param name="SQLString"></param>
  27. /// <returns></returns>
  28. public static int ExecuteSql(string SQLString)
  29. {
  30. using (SqlConnection connection = new SqlConnection(connectionString))
  31. {
  32. using (SqlCommand cmd = new SqlCommand(SQLString,connection))
  33. {
  34. try
  35. {
  36. connection.Open();
  37. int rows=cmd.ExecuteNonQuery();
  38. return rows;
  39. }
  40. catch(System.Data.SqlClient.SqlException E)
  41. {
  42. throw new Exception(E.Message);
  43. }
  44. }
  45. }
  46. }
  47. /// <summary>
  48. /// 执行两条SQL语句,实现数据库事务。
  49. /// </summary>
  50. /// <param name="SQLString1"></param>
  51. /// <param name="SQLString2"></param>
  52. public static void ExecuteSqlTran(string SQLString1,string SQLString2)
  53. {
  54. using (SqlConnection connection = new SqlConnection(connectionString))
  55. {
  56. connection.Open();
  57. SqlCommand cmd = new SqlCommand();
  58. cmd.Connection=connection;
  59. SqlTransaction tx=connection.BeginTransaction();
  60. cmd.Transaction=tx;
  61. try
  62. {
  63. cmd.CommandText=SQLString1;
  64. cmd.ExecuteNonQuery();
  65. cmd.CommandText=SQLString2;
  66. cmd.ExecuteNonQuery();
  67. tx.Commit();
  68. }
  69. catch(System.Data.SqlClient.SqlException E)
  70. {
  71. tx.Rollback();
  72. throw new Exception(E.Message);
  73. }
  74. finally
  75. {
  76. cmd.Dispose();
  77. connection.Close();
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 执行多条SQL语句,实现数据库事务,每条语句以“;”分割。
  83. /// </summary>
  84. /// <param name="SQLStringList"></param>
  85. public static void ExecuteSqlTran(string SQLStringList)
  86. {
  87. using (OdbcConnection conn = new OdbcConnection(connectionString))
  88. {
  89. conn.Open();
  90. OdbcCommand cmd = new OdbcCommand();
  91. cmd.Connection=conn;
  92. OdbcTransaction tx=conn.BeginTransaction();
  93. cmd.Transaction=tx;
  94. try
  95. {
  96. string [] split= SQLStringList.Split(new Char [] { ';'});
  97. foreach (string strsql in split)
  98. {
  99. if (strsql.Trim()!="")
  100. {
  101. cmd.CommandText=strsql;
  102. cmd.ExecuteNonQuery();
  103. }
  104. }
  105. tx.Commit();
  106. }
  107. catch(System.Data.Odbc.OdbcException E)
  108. {
  109. tx.Rollback();
  110. throw new Exception(E.Message);
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// 执行带一个存储过程参数的的SQL语句。
  116. /// </summary>
  117. /// <param name="SQLString"></param>
  118. /// <param name="content"></param>
  119. /// <returns></returns>
  120. public static int ExecuteSql(string SQLString,string content)
  121. {
  122. using (SqlConnection connection = new SqlConnection(connectionString))
  123. {
  124. SqlCommand cmd = new SqlCommand(SQLString,connection);
  125. System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter ( "@content", SqlDbType.NText);
  126. myParameter.Value = content ;
  127. cmd.Parameters.Add(myParameter);
  128. try
  129. {
  130. connection.Open();
  131. int rows=cmd.ExecuteNonQuery();
  132. return rows;
  133. }
  134. catch(System.Data.SqlClient.SqlException E)
  135. {
  136. throw new Exception(E.Message);
  137. }
  138. finally
  139. {
  140. cmd.Dispose();
  141. connection.Close();
  142. }
  143. }
  144. }
  145. /// <summary>
  146. /// 向数据库里插入图像格式的字段
  147. /// </summary>
  148. /// <param name="strSQL"></param>
  149. /// <param name="fs"></param>
  150. /// <returns></returns>
  151. public static int ExecuteSqlInsertImg(string strSQL,byte[] fs)
  152. {
  153. using (SqlConnection connection = new SqlConnection(connectionString))
  154. {
  155. SqlCommand cmd = new SqlCommand(strSQL,connection);
  156. System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter ( "@fs", SqlDbType.Image);
  157. myParameter.Value = fs ;
  158. cmd.Parameters.Add(myParameter);
  159. try
  160. {
  161. connection.Open();
  162. int rows=cmd.ExecuteNonQuery();
  163. return rows;
  164. }
  165. catch(System.Data.SqlClient.SqlException E)
  166. {
  167. throw new Exception(E.Message);
  168. }
  169. finally
  170. {
  171. cmd.Dispose();
  172. connection.Close();
  173. }
  174.  
  175. }
  176. }
  177. /// <summary>
  178. /// 执行一条计算查询结果语句,返回查询结果(整数)。
  179. /// </summary>
  180. /// <param name="strSQL"></param>
  181. /// <returns></returns>
  182. public static int GetCount(string strSQL)
  183. {
  184. using (SqlConnection connection = new SqlConnection(connectionString))
  185. {
  186. SqlCommand cmd = new SqlCommand(strSQL,connection);
  187. try
  188. {
  189. connection.Open();
  190. SqlDataReader result = cmd.ExecuteReader();
  191. int i=0;
  192. while(result.Read())
  193. {
  194. i=result.GetInt32(0);
  195. }
  196. result.Close();
  197. return i;
  198. }
  199. catch(System.Data.SqlClient.SqlException e)
  200. {
  201. throw new Exception(e.Message);
  202. }
  203. finally
  204. {
  205. cmd.Dispose();
  206. connection.Close();
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// 执行一条计算查询结果语句,返回查询结果(object)。
  212. /// </summary>
  213. /// <param name="SQLString"></param>
  214. /// <returns></returns>
  215. public static object GetSingle(string SQLString)
  216. {
  217. using (SqlConnection connection = new SqlConnection(connectionString))
  218. {
  219. SqlCommand cmd = new SqlCommand(SQLString,connection);
  220. try
  221. {
  222. connection.Open();
  223. object obj = cmd.ExecuteScalar();
  224. if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
  225. {
  226. return null;
  227. }
  228. else
  229. {
  230. return obj;
  231. }
  232. }
  233. catch(System.Data.SqlClient.SqlException e)
  234. {
  235. throw new Exception(e.Message);
  236. }
  237. finally
  238. {
  239. cmd.Dispose();
  240. connection.Close();
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// 执行查询语句,返回SqlDataReader
  246. /// </summary>
  247. /// <param name="strSQL"></param>
  248. /// <returns></returns>
  249. public static SqlDataReader ExecuteReader(string strSQL)
  250. {
  251. using (SqlConnection connection = new SqlConnection(connectionString))
  252. {
  253. SqlCommand cmd = new SqlCommand(strSQL,connection);
  254. SqlDataReader myReader;
  255. try
  256. {
  257. connection.Open();
  258. myReader = cmd.ExecuteReader();
  259. return myReader;
  260. }
  261. catch(System.Data.SqlClient.SqlException e)
  262. {
  263. throw new Exception(e.Message);
  264. }
  265. finally
  266. {
  267. cmd.Dispose();
  268. connection.Close();
  269. }
  270. }
  271. }
  272. /// <summary>
  273. /// 执行查询语句,返回DataSet
  274. /// </summary>
  275. /// <param name="SQLString"></param>
  276. /// <returns></returns>
  277. public static DataSet Query(string SQLString)
  278. {
  279. using (SqlConnection connection = new SqlConnection(connectionString))
  280. {
  281. DataSet ds = new DataSet();
  282. try
  283. {
  284. connection.Open();
  285. SqlDataAdapter command = new SqlDataAdapter(SQLString,connection);
  286. command.Fill(ds,"ds");
  287. }
  288. catch(System.Data.SqlClient.SqlException ex)
  289. {
  290. throw new Exception(ex.Message);
  291. }
  292. return ds;
  293. }
  294.  
  295. }
  296.  
  297. #region 存储过程操作
  298.  
  299. /// <summary>
  300. /// 运行存储过程
  301. /// </summary>
  302. /// <param name="storedProcName"></param>
  303. /// <param name="parameters"></param>
  304. /// <returns></returns>
  305. public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
  306. {
  307. using (SqlConnection connection = new SqlConnection(connectionString))
  308. {
  309. SqlDataReader returnReader;
  310. connection.Open();
  311. SqlCommand command = BuildQueryCommand( connection,storedProcName, parameters );
  312. command.CommandType = CommandType.StoredProcedure;
  313.  
  314. returnReader = command.ExecuteReader();
  315. //Connection.Close();
  316. return returnReader;
  317. }
  318. }
  319. private static SqlCommand BuildQueryCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters)
  320. {
  321.  
  322. SqlCommand command = new SqlCommand( storedProcName, connection );
  323. command.CommandType = CommandType.StoredProcedure;
  324. foreach (SqlParameter parameter in parameters)
  325. {
  326. command.Parameters.Add( parameter );
  327. }
  328. return command;
  329.  
  330. }
  331. public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
  332. {
  333. using (SqlConnection connection = new SqlConnection(connectionString))
  334. {
  335. DataSet dataSet = new DataSet();
  336. connection.Open();
  337. SqlDataAdapter sqlDA = new SqlDataAdapter();
  338. sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters );
  339. sqlDA.Fill( dataSet, tableName );
  340. connection.Close();
  341.  
  342. return dataSet;
  343. }
  344. }
  345.  
  346. #endregion
  347. }
  348. }
  349.  
  350. --------------------------------------------------------------------------------------------------------------------------------
  351.  
  352. //这是我更新的一个版本,与上面可以说变化相当大
  353.  
  354. //其中FlashGateway是Flash Remoting的一个dll大家可以去掉相关代码,不影响使用
  355.  
  356. using System;
  357. using System.Collections.Generic;
  358. using System.Text;
  359. using System.Collections;
  360. using System.Collections.Specialized;
  361. using System.Data;
  362. using System.Data.SqlClient;
  363. using System.Drawing;
  364. using mvi.sysmanage;
  365. using FlashGateway.IO;
  366.  
  367. namespace mvi.dbaccess
  368. {
  369. #region class sqlcom
  370. /// <summary>
  371. /// sqlcom
  372. /// </summary>
  373. public class sqlCom
  374. {
  375. //FLASHSRV/HIPIHI
  376. //private string DBCnStr = @"Data Source=FLASHSRV;Initial Catalog=hipihi;Persist Security Info=True;User ID=sa;password=1";
  377.  
  378. private string DBCnStr = @"Data Source=mvi-dpe;Initial Catalog=hipihi;Persist Security Info=True;User ID=sa;password=mvi";
  379.  
  380. public sqlCom()
  381. {
  382. //DBCnStr = @"Data Source=FLASHSRV;Initial Catalog=hipihi;Persist Security Info=True;User ID=sa;password=1";
  383. DBCnStr = @"Data Source=mvi-dpe;Initial Catalog=hipihi;Persist Security Info=True;User ID=sa;password=mvi";
  384. }
  385.  
  386. public sqlCom(string connectstring)
  387. {
  388. if (connectstring.Length > 1)
  389. {
  390. DBCnStr = connectstring;
  391. }
  392. }
  393.  
  394. public string SQLCnStr
  395. {
  396. get
  397. {
  398. return DBCnStr;
  399. }
  400. set
  401. {
  402. DBCnStr = value;
  403. }
  404. }
  405.  
  406. #region InitSqlConnection 初始化Sql连接字符串
  407. /// <summary>
  408. /// 初始化Sql连接字符串
  409. /// </summary>
  410. /// <param name="DBCnStr">传入的dbconnection</param>
  411. /// <param name="cmdText">传入的cmd text</param>
  412. /// <returns>sql server connection string</returns>
  413. private string InitSqlConnection(string DBCnStr, string cmdText)
  414. {
  415. // temp code
  416. return DBCnStr;
  417.  
  418. //// final code
  419. //int iD = DBCnStr.IndexOf("Data Source=");
  420. //int iL = DBCnStr.Substring(iD + 12).Split(';')[0].Length;
  421. //string strSqlServerName = DBCnStr.Substring(iD + 12, iL);
  422. //string strNewSqlServerName = GetSqlServerName(cmdText);
  423. //return DBCnStr.Replace(strSqlServerName, strNewSqlServerName);
  424. }
  425. #endregion
  426.  
  427. #region GetSqlServerName 由sql string 获取数据库服务器名
  428. /// <summary>
  429. /// 由sql string 获取sql server name
  430. /// </summary>
  431. /// <param name="cmdText">传入的cmd text</param>
  432. /// <returns>sql server name</returns>
  433. private string GetSqlServerName(string cmdText)
  434. {
  435. return cmdText.Substring(cmdText.IndexOf("from") + 5).Split('.')[0].ToString();
  436. }
  437. #endregion
  438.  
  439. # region GetDataSet 通过执行SQL语句返回一个状态
  440. /// <summary>
  441. /// 通过执行SQL语句返回一个状态
  442. /// </summary>
  443. /// <param name="cmdText">“SQL 文本”</param>
  444. /// <param name="oCn">"连接对象"</param>
  445. /// <param name="oDs">"引用的DataSet它将在程序中改变内容"</param>
  446. /// <returns>"成功则返回0,否则返回错误代码"</returns>
  447. public int GetDataSet(string cmdText, ref DataSet oDs)
  448. {
  449. DBCnStr = this.InitSqlConnection(DBCnStr, cmdText);
  450. SqlConnection oCn = new SqlConnection(DBCnStr);
  451.  
  452. try
  453. {
  454. oCn.Open();
  455. }
  456. catch (Exception oErr)
  457. {
  458. //WriteFile(oErr.Message);
  459. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  460.  
  461. return -1;
  462. }
  463.  
  464. //定义数据适配对象
  465. SqlDataAdapter oleDataAdapter = new SqlDataAdapter(cmdText, oCn);
  466.  
  467. int status = -1;
  468. try
  469. {
  470. //填充DataSet
  471. oleDataAdapter.Fill(oDs);
  472. status = 0;
  473. }
  474. catch (Exception oErr)
  475. {
  476. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  477. status = -1;
  478. }
  479. finally
  480. {
  481. oleDataAdapter = null;
  482. if (oCn.State == System.Data.ConnectionState.Open)
  483. {
  484. oCn.Close();
  485. }
  486. }
  487. return status;
  488. }
  489. #endregion
  490.  
  491. # region GetDataTable 执行SQL语句并返回一个表
  492. /// <summary>
  493. /// 执行SQL语句并返回一个表
  494. /// </summary>
  495. /// <param name="cmdText">SQL文本</param>
  496. /// <param name="DBCnStr">dbconnect</param>
  497. /// <param name="inDt">返回表</param>
  498. /// <returns>成功则返回0,否则返回错误代码</returns>
  499. public int GetDataTable(string cmdText, ref DataTable inDt)
  500. {
  501. DBCnStr = this.InitSqlConnection(DBCnStr, cmdText);
  502. SqlConnection oCn = new SqlConnection(DBCnStr);
  503.  
  504. try
  505. {
  506. oCn.Open();
  507. }
  508. catch (Exception oErr)
  509. {
  510. //WriteFile(oErr.Message);
  511. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  512.  
  513. return -1;
  514. }
  515.  
  516. //建立数据适配对象
  517. SqlDataAdapter oleDataAdapter = new SqlDataAdapter(cmdText, oCn);
  518.  
  519. int status = -1;
  520. try
  521. {
  522. //填充数据表
  523. oleDataAdapter.Fill(inDt);
  524. status = 0;
  525. }
  526. catch (Exception oErr)
  527. {
  528. //异常处理
  529. //WriteFile(oErr.Message);
  530. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  531.  
  532. status = -1;
  533. }
  534. finally
  535. {
  536. oleDataAdapter = null;
  537. if (oCn.State == System.Data.ConnectionState.Open)
  538. {
  539. oCn.Close();
  540. }
  541. }
  542. return status;
  543. }
  544. #endregion
  545.  
  546. # region GetDataTable 执行SQL语句并返回一个表
  547. /// <summary>
  548. /// 执行SQL语句并返回一个表
  549. /// </summary>
  550. /// <param name="cmdText">SQL文本</param>
  551. /// <param name="DBCnStr">dbconnect</param>
  552. /// <param name="inDt">返回表</param>
  553. /// <returns>成功则返回0,否则返回错误代码</returns>
  554. public int GetCount(string cmdText)
  555. {
  556. DBCnStr = this.InitSqlConnection(DBCnStr, cmdText);
  557. SqlConnection oCn = new SqlConnection(DBCnStr);
  558.  
  559. try
  560. {
  561. oCn.Open();
  562. }
  563. catch (Exception oErr)
  564. {
  565. //WriteFile(oErr.Message);
  566. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  567.  
  568. return -1;
  569. }
  570.  
  571. //建立数据适配对象
  572. SqlDataAdapter oleDataAdapter = new SqlDataAdapter(cmdText, oCn);
  573.  
  574. DataTable inDt = new DataTable();
  575. int status = -1;
  576. try
  577. {
  578. //填充数据表
  579. oleDataAdapter.Fill(inDt);
  580.  
  581. status = inDt.Rows.Count;
  582. }
  583. catch (Exception oErr)
  584. {
  585. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  586. status = -1;
  587. }
  588. finally
  589. {
  590. oleDataAdapter = null;
  591. if (oCn.State == System.Data.ConnectionState.Open)
  592. {
  593. oCn.Close();
  594. }
  595. }
  596. return status;
  597. }
  598. #endregion
  599.  
  600. # region // GetNVColl 执行SQL语句并返回NameValueCollection
  601. ///// <summary>
  602. ///// 执行SQL语句并返回NameValueCollection
  603. ///// </summary>
  604. ///// <param name="cmdText">SQL文本</param>
  605. ///// <param name="NameValueCollection">nvColl</param>
  606. ///// <returns>成功则返回0,否则返回错误代码</returns>
  607. //public int GetNVColl(string cmdText, ref NameValueCollection nvColl)
  608. //{
  609.  
  610. // DBCnStr = this.InitSqlConnection(DBCnStr, cmdText);
  611. // SqlConnection oCn = new SqlConnection(DBCnStr);
  612.  
  613. // try
  614. // {
  615. // oCn.Open();
  616. // }
  617. // catch (Exception oErr)
  618. // {
  619. // //WriteFile(oErr.Message);
  620. // Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  621.  
  622. // return -1;
  623. // }
  624. // //建立数据读取对象
  625. // SqlCommand oleCommand = new SqlCommand(cmdText, oCn);
  626. // //填充SqlDataReader
  627. // SqlDataReader oleReader;
  628.  
  629. // int status = -1;
  630. // try
  631. // {
  632.  
  633. // oleReader = oleCommand.ExecuteReader();
  634. // // Always call Read before accessing data.
  635. // while (oleReader.Read())
  636. // {
  637. // for (int i = 0; i < oleReader.FieldCount; i++)
  638. // {
  639. // if (oleReader.GetValue(i).ToString() != "")
  640. // nvColl.Add(oleReader.GetName(i), oleReader.GetString(i));
  641. // }
  642. // }
  643.  
  644. // status = 0;
  645. // }
  646. // catch (Exception oErr)
  647. // {
  648. // //异常处理
  649. // //WriteFile(oErr.Message);
  650. // Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  651.  
  652. // status = -1;
  653. // }
  654. // finally
  655. // {
  656. // oleReader = null;
  657. // if (oCn.State == System.Data.ConnectionState.Open)
  658. // {
  659. // oCn.Close();
  660. // }
  661. // }
  662. // return status;
  663. //}
  664. #endregion
  665.  
  666. //
  667. #region GetArrayList 执行SQL语句并返回一个ArrayList
  668. ///// <summary>
  669. ///// 执行SQL语句并返回一个ArrayList
  670. ///// </summary>
  671. ///// <param name="cmdText">SQL文本</param>
  672. ///// <returns>返回ArrayList arraylist[i]为name,arraylist[i+1]为value</returns>
  673. //public ArrayList GetArrayList(string cmdText, ref ArrayList aName, ref ArrayList aValue)
  674. //{
  675. // ArrayList aNameValue = new ArrayList();
  676. // SqlConnection oCn = new SqlConnection(DBCnStr);
  677.  
  678. // try
  679. // {
  680. // oCn.Open();
  681. // }
  682. // catch (Exception oErr)
  683. // {
  684. // //WriteFile(oErr.Message);
  685. // Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  686.  
  687. // return null;
  688. // }
  689. // //建立数据读取对象
  690. // SqlCommand oleCommand = new SqlCommand(cmdText, oCn);
  691. // //填充SqlDataReader
  692. // SqlDataReader oleReader;
  693.  
  694. // ArrayList status = null;
  695. // try
  696. // {
  697.  
  698. // oleReader = oleCommand.ExecuteReader();
  699. // // Always call Read before accessing data.
  700. // while (oleReader.Read())
  701. // {
  702. // for (int i = 0; i < oleReader.FieldCount; i++)
  703. // {
  704. // if (oleReader.GetValue(i).ToString() != "")
  705. // aName.Add(oleReader.GetName(i));
  706. // aValue.Add(oleReader.GetString(i + 1));
  707. // }
  708. // }
  709.  
  710. // status = aValue;
  711. // }
  712. // catch (Exception oErr)
  713. // {
  714. // //异常处理
  715. // //WriteFile(oErr.Message);
  716. // Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  717.  
  718. // status = null;
  719. // }
  720. // finally
  721. // {
  722. // oleReader = null;
  723. // if (oCn.State == System.Data.ConnectionState.Open)
  724. // {
  725. // oCn.Close();
  726. // }
  727. // }
  728. // return status;
  729. //}
  730. #endregion
  731. //
  732.  
  733. #region GetArrayList 执行SQL语句并返回一个ArrayList
  734. /// <summary>
  735. /// 执行SQL语句并返回一个ArrayList
  736. /// </summary>
  737. /// <param name="cmdText">SQL文本</param>
  738. /// <returns>返回ArrayList arraylist[i]为name,arraylist[i+1]为value</returns>
  739. public int GetArrayList(string cmdText, ref ArrayList aNameValue)
  740. {
  741. DBCnStr = this.InitSqlConnection(DBCnStr, cmdText);
  742. SqlConnection oCn = new SqlConnection(DBCnStr);
  743.  
  744. try
  745. {
  746. oCn.Open();
  747. }
  748. catch (Exception oErr)
  749. {
  750. //WriteFile(oErr.Message);
  751. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  752.  
  753. return -1;
  754. }
  755. //建立数据读取对象
  756. SqlCommand oleCommand = new SqlCommand(cmdText, oCn);
  757. //填充SqlDataReader
  758. SqlDataReader oleReader;
  759.  
  760. int status = -1;
  761. try
  762. {
  763.  
  764. oleReader = oleCommand.ExecuteReader();
  765. // Always call Read before accessing data.
  766. while (oleReader.Read())
  767. {
  768. for (int i = 0; i < oleReader.FieldCount - 1; i ++ )
  769. {
  770. if (oleReader.GetValue(i).ToString() != "")
  771. aNameValue.Add(oleReader.GetName(i));
  772. }
  773. }
  774.  
  775. status = 1;
  776. }
  777. catch (Exception oErr)
  778. {
  779. //异常处理
  780. //WriteFile(oErr.Message);
  781. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  782.  
  783. status = -1;
  784. }
  785. finally
  786. {
  787. oleReader = null;
  788. if (oCn.State == System.Data.ConnectionState.Open)
  789. {
  790. oCn.Close();
  791. }
  792. }
  793. return status;
  794. }
  795. #endregion
  796.  
  797. #region GetASObject 执行SQL语句并返回一个包含多条数据的ASObject (name,value)
  798. /// <summary>
  799. /// 执行SQL语句,查询两个字段,并返回一个ASObject
  800. /// </summary>
  801. /// <param name="cmdText">SQL文本</param>
  802. /// <param name="asO">ASObject 对象</param>
  803. /// <returns>返回int ASObject[i]为(name,value)</returns>
  804. public int GetASObjectMulti(string cmdText, ref ASObject asO)
  805. {
  806. DBCnStr = this.InitSqlConnection(DBCnStr, cmdText);
  807. SqlConnection oCn = new SqlConnection(DBCnStr);
  808. try
  809. {
  810. oCn.Open();
  811. }
  812. catch (Exception oErr)
  813. {
  814. //WriteFile(oErr.Message);
  815. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  816.  
  817. return -1;
  818. }
  819. //建立数据读取对象
  820. SqlCommand oleCommand = new SqlCommand(cmdText, oCn);
  821. //填充SqlDataReader
  822. SqlDataReader oleReader;
  823.  
  824. int status = -1;
  825. try
  826. {
  827. int i = 1;
  828. oleReader = oleCommand.ExecuteReader();
  829. // Always call Read before accessing data.
  830. while (oleReader.Read())
  831. {
  832. for (int j = 0; j < oleReader.FieldCount; j++)
  833. {
  834. asO.Add(i+"@"+oleReader.GetName(j),oleReader.GetValue(j));//i@+"name",i为第几条数据的序号
  835. }
  836. i++;
  837. }
  838.  
  839. status = 1;
  840. }
  841. catch (Exception oErr)
  842. {
  843. //异常处理
  844. //WriteFile(oErr.Message);
  845. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  846.  
  847. status = -1;
  848. }
  849. finally
  850. {
  851. oleReader = null;
  852. if (oCn.State == System.Data.ConnectionState.Open)
  853. {
  854. oCn.Close();
  855. }
  856. }
  857. return status;
  858. }
  859. #endregion
  860.  
  861. #region GetASObjectSingle 执行SQL语句并返回一个包含单条数据的ASObject (name,value)
  862. /// <summary>
  863. /// 执行SQL语句查询一条数据(必须返回一条数据),返回一个ASObject
  864. /// </summary>
  865. /// <param name="cmdText">SQL文本</param>
  866. /// <param name="asO">ASObject 对象</param>
  867. /// <returns>返回int ASObject[i]为(name,value)</returns>
  868. public int GetASObjectSingle(string cmdText, ref ASObject asO)
  869. {
  870. DBCnStr = this.InitSqlConnection(DBCnStr, cmdText);
  871. SqlConnection oCn = new SqlConnection(DBCnStr);
  872. try
  873. {
  874. oCn.Open();
  875. }
  876. catch (Exception oErr)
  877. {
  878. //WriteFile(oErr.Message);
  879. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  880.  
  881. return -1;
  882. }
  883. //建立数据读取对象
  884. SqlCommand oleCommand = new SqlCommand(cmdText, oCn);
  885. //填充SqlDataReader
  886. SqlDataReader oleReader;
  887.  
  888. int status = -1;
  889. try
  890. {
  891. //oleReader = oleCommand.ExecuteScalar(); // modified by apenni 2006-5-6
  892. oleReader = oleCommand.ExecuteReader();
  893. // Always call Read before accessing data.
  894. while (oleReader.Read())
  895. {
  896. for (int i = 0; i < oleReader.FieldCount; i++)
  897. {
  898. asO.Add(oleReader.GetName(i), oleReader.GetValue(i));
  899. }
  900. }
  901.  
  902. status = 1;
  903. }
  904. catch (Exception oErr)
  905. {
  906. //异常处理
  907. //WriteFile(oErr.Message);
  908. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  909.  
  910. status = -1;
  911. }
  912. finally
  913. {
  914. oleReader = null;
  915. if (oCn.State == System.Data.ConnectionState.Open)
  916. {
  917. oCn.Close();
  918. }
  919. }
  920. return status;
  921. }
  922. #endregion
  923.  
  924. #region ExecuteSql 执行SET,DELETE语句时返回影响的行数
  925. /// <summary>
  926. /// 执行SET,DELETE语句时返回影响的行数
  927. /// </summary>
  928. /// <param name="cmdText">“SQL文本”</param>
  929. /// <returns>“返回影响的行数,否则返回错误代码”</returns>
  930. public int ExecuteSql(string cmdText)
  931. {
  932. int intReturn = -1;//返回影响的行数。
  933. SqlCommand oCmd = new SqlCommand();
  934.  
  935. DBCnStr = this.InitSqlConnection(DBCnStr, cmdText);
  936. SqlConnection oCn = new SqlConnection(DBCnStr);
  937. try
  938. {
  939. oCn.Open();
  940. }
  941. catch (Exception oErr)
  942. {
  943. //WriteFile(oErr.Message);
  944. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  945.  
  946. return -1;
  947. }
  948.  
  949. oCmd.Connection = oCn;
  950. oCmd.CommandType = CommandType.Text;
  951. oCmd.CommandText = cmdText;
  952.  
  953. //定义事务 设定隔离级别
  954. SqlTransaction oTx = oCn.BeginTransaction(IsolationLevel.ReadCommitted);
  955. oCmd.Transaction = oTx;
  956.  
  957. //处理SQL语句
  958. #region 事务处理
  959. try
  960. {
  961. //支持事务
  962. intReturn = oCmd.ExecuteNonQuery();
  963. oTx.Commit();
  964.  
  965. }
  966. catch (Exception oErr)
  967. {
  968. //WriteFile(oErr.Message);
  969. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  970.  
  971. oTx.Rollback();
  972.  
  973. intReturn = -1;
  974.  
  975. }
  976. finally
  977. {
  978. oCmd = null;
  979. oTx = null;
  980. if (oCn.State == System.Data.ConnectionState.Open)
  981. {
  982. oCn.Close();
  983. }
  984. }
  985. #endregion
  986.  
  987. return intReturn;
  988. }
  989. #endregion
  990.  
  991. #region ExecuteSql 执行SET,DELETE语句时返回影响的行数
  992. /// <summary>
  993. /// 执行SET,DELETE语句时返回影响的行数
  994. /// </summary>
  995. /// <param name="cmdText">“SQL文本,支持多sql语句通过';'拼接”</param>
  996. /// <returns>“返回影响的行数,否则返回错误代码”</returns>
  997. public int ExecuteSql(params string[] cmdText)
  998. {
  999. string strSql = string.Empty;
  1000. foreach (string strCmd in cmdText)
  1001. {
  1002. strSql += strCmd;
  1003. }
  1004. return ExecuteSql(strSql);
  1005. }
  1006. //added by apenni 2006-5-6
  1007. #endregion
  1008.  
  1009. #region CallStoreProc 调用系统存储过程返回一个整数
  1010. /// <summary>
  1011. /// 调用系统存储过程返回一个整数
  1012. /// </summary>
  1013. /// <param name = "strSysSPName">“存储过程枚举类型”</param>
  1014. /// <param name="InParaName">"in参数名字"</param>
  1015. /// <param name = "ParamValue">“参数列表”</param>
  1016. /// <param name="OutParaName">"out参数名字"</param>
  1017. /// <param name="OutParaValue">"返回的参数值"</param>
  1018. /// <param name="IType">"out参数的类型"</param>
  1019. /// <returns>"成功则返回所影响的行数,否则返回-1"</returns>
  1020. public int CallStoreProc(string strSysSPName, IList InParaName, IList InParamValue, IList OutParaName, ref object[] OutParaValue, DBTYPE[] IType)
  1021. {
  1022. int inReturn = -1;
  1023.  
  1024. SqlCommand oCmd = new SqlCommand();
  1025.  
  1026. oCmd.CommandText = strSysSPName;
  1027. oCmd.CommandType = CommandType.StoredProcedure;
  1028.  
  1029. #region in参数的建立
  1030. if (InParamValue != null && InParaName != null)
  1031. {
  1032. //建立in参数
  1033. for (int i = 0; i < InParamValue.Count; i++)
  1034. {
  1035. SqlParameter oPara = new SqlParameter();
  1036.  
  1037. //调用SQLParamHelper的CreateParameterWithValue()方法来生成不同的参数
  1038. if (InParaName[i] != null)
  1039. {
  1040. oPara = SQLParamHelper.CreateParameterWithValue(InParaName[i].ToString(), InParamValue[i]);
  1041. oPara.Direction = ParameterDirection.Input;
  1042. oCmd.Parameters.Add(oPara);
  1043. }
  1044. }
  1045. }
  1046. #endregion
  1047.  
  1048. #region out参数的建立
  1049.  
  1050. if (OutParaName != null && OutParaValue != null && IType != null)
  1051. {
  1052. //建立in参数
  1053. for (int i = 0; i < OutParaName.Count; i++)
  1054. {
  1055. SqlParameter oPara = new SqlParameter();
  1056.  
  1057. //调用SQLParamHelper的CreateParameterWithValue()方法来生成不同的参数
  1058. if (OutParaName[i] != null)
  1059. {
  1060. oPara = SQLParamHelper.CreateOutParameterWithValue(OutParaName[i].ToString(), IType[i].ToString());
  1061. oPara.Direction = ParameterDirection.Output;
  1062. oCmd.Parameters.Add(oPara);
  1063. }
  1064. }
  1065. }
  1066.  
  1067. #endregion
  1068.  
  1069. SqlConnection oCn = new SqlConnection(DBCnStr);
  1070. try
  1071. {
  1072. oCn.Open();
  1073. }
  1074. catch (Exception oErr)
  1075. {
  1076. //WriteFile(oErr.Message);
  1077. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  1078. return -1;
  1079. }
  1080.  
  1081. oCmd.Connection = oCn;
  1082.  
  1083. //连接数据库和执行存储过程
  1084. try
  1085. {
  1086. inReturn = oCmd.ExecuteNonQuery();
  1087. for (int i = 0; i < OutParaValue.Length; i++)
  1088. {
  1089. OutParaValue[i] = oCmd.Parameters[OutParaName[i].ToString()].Value;
  1090. }
  1091. }
  1092. catch (Exception oErr)
  1093. {
  1094. //WriteFile(oErr.Message);
  1095. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  1096.  
  1097. inReturn = -1;
  1098. }
  1099. finally
  1100. {
  1101. if (oCn.State == System.Data.ConnectionState.Open)
  1102. {
  1103. oCn.Close();
  1104. }
  1105. oCmd = null;
  1106. }
  1107. return inReturn;
  1108. }
  1109. #endregion
  1110.  
  1111. #region CallStoreProc 调用系统存储过程并影响生成一个object对象值
  1112. /// <summary>
  1113. /// 调用系统存储过程并影响生成一个object对象值
  1114. /// </summary>
  1115. /// <param name = "strSysSPName">“存储过程枚举类型”</param>
  1116. /// <param name="InParaName">"in参数名字"</param>
  1117. /// <param name = "InParamValue">“in参数列表”</param>
  1118. /// <param name="OutParaName">"out参数名字"</param>
  1119. /// <param name="OutParaValue">"out参数值"</param>
  1120. /// <param name="IType">"out参数的类型"</param>
  1121. /// <param name="inObject">"引用的值"</param>
  1122. /// <returns>成功则返回1,否则返回-1或错误代码</returns>
  1123. public int CallStoreProc(string strSysSPName, IList InParaName, IList InParamValue, IList OutParaName, ref object[] OutParaValue, DBTYPE[] IType, ref object objReturn)
  1124. {
  1125. //建立Command对象
  1126. SqlCommand oCmd = new SqlCommand();
  1127.  
  1128. oCmd.CommandText = strSysSPName.ToString();
  1129. oCmd.CommandType = CommandType.StoredProcedure;
  1130.  
  1131. int status = -1;
  1132.  
  1133. #region in参数的建立
  1134. if (InParamValue != null && InParaName != null)
  1135. {
  1136. //建立in参数
  1137. for (int i = 0; i < InParamValue.Count; i++)
  1138. {
  1139. SqlParameter oPara = new SqlParameter();
  1140.  
  1141. //调用SQLParamHelper的CreateParameterWithValue()方法来生成不同的参数
  1142. if (InParaName[i] != null)
  1143. {
  1144. oPara = SQLParamHelper.CreateParameterWithValue(InParaName[i].ToString(), InParamValue[i]);
  1145. oPara.Direction = ParameterDirection.Input;
  1146. oCmd.Parameters.Add(oPara);
  1147. }
  1148. }
  1149. }
  1150. #endregion
  1151.  
  1152. #region out参数的建立
  1153.  
  1154. if (OutParaName != null && OutParaValue != null && IType != null)
  1155. {
  1156. //建立in参数
  1157. for (int i = 0; i < OutParaName.Count; i++)
  1158. {
  1159. SqlParameter oPara = new SqlParameter();
  1160.  
  1161. //调用SQLParamHelper的CreateParameterWithValue()方法来生成不同的参数
  1162. if (OutParaName[i] != null)
  1163. {
  1164. oPara = SQLParamHelper.CreateOutParameterWithValue(OutParaName[i].ToString(), IType[i].ToString());
  1165. oPara.Direction = ParameterDirection.Output;
  1166. oCmd.Parameters.Add(oPara);
  1167. }
  1168. }
  1169. }
  1170.  
  1171. #endregion
  1172.  
  1173. SqlConnection oCn = new SqlConnection(DBCnStr);
  1174. try
  1175. {
  1176. oCn.Open();
  1177. }
  1178. catch (Exception oErr)
  1179. {
  1180. //WriteFile(oErr.Message);
  1181. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  1182.  
  1183. return -1;
  1184. }
  1185.  
  1186. oCmd.Connection = oCn;
  1187.  
  1188. //连接数据库和执行存储过程
  1189. try
  1190. {
  1191. //通过SqlDataAdapter来填充Table
  1192. objReturn = oCmd.ExecuteScalar();
  1193. #region 取得返回参数的值
  1194.  
  1195. for (int i = 0; i < OutParaValue.Length; i++)
  1196. {
  1197. OutParaValue[i] = oCmd.Parameters[OutParaName[i].ToString()].Value;
  1198. }
  1199.  
  1200. #endregion
  1201. status = 0;
  1202. }
  1203. catch (Exception oErr)
  1204. {
  1205. //WriteFile(oErr.Message);
  1206. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  1207.  
  1208. status = -1;
  1209. }
  1210. finally
  1211. {
  1212. if (oCn.State == System.Data.ConnectionState.Open)
  1213. {
  1214. oCn.Close();
  1215. }
  1216. oCmd = null;
  1217. }
  1218. return status;
  1219. }
  1220. #endregion
  1221.  
  1222. #region CallStoreProc 调用用户存储过程返回一个DataTable(Select 语句)
  1223. /// <summary>
  1224. /// 调用用户存储过程返回一个DataTable(Select 语句)
  1225. /// </summary>
  1226. /// <param name = "strSPName">“存储过程名”</param>
  1227. /// <param name="InParaName">"in参数名字"</param>
  1228. /// <param name = "InParamValue">“in参数列表”</param>
  1229. /// <param name="OutParaName">"out参数名字"</param>
  1230. /// <param name="IType">"out参数的类型"</param>
  1231. /// <param name="OutParaValue">"out参数值"</param>
  1232. /// <param name="oDT">"传入的DataTable引用"</param>
  1233. ///<returns>"成功则返回1,否则返回-1或错误代码"</returns>
  1234. public int CallStoreProc(string strSPName, IList InParaName, IList InParamValue, IList OutParaName, ref object[] OutParaValue, DBTYPE[] IType, ref DataTable oDT)
  1235. {
  1236.  
  1237. //建立Command对象
  1238. SqlCommand oCmd = new SqlCommand();
  1239.  
  1240. oCmd.CommandText = strSPName.ToString();
  1241. oCmd.CommandType = CommandType.StoredProcedure;
  1242.  
  1243. int status = -1;
  1244.  
  1245. #region in参数的建立
  1246. if (InParamValue != null && InParaName != null)
  1247. {
  1248. //建立in参数
  1249. for (int i = 0; i < InParamValue.Count; i++)
  1250. {
  1251. SqlParameter oPara = new SqlParameter();
  1252.  
  1253. //调用SQLParamHelper的CreateParameterWithValue()方法来生成不同的参数
  1254. if (InParaName[i] != null)
  1255. {
  1256. oPara = SQLParamHelper.CreateParameterWithValue(InParaName[i].ToString(), InParamValue[i]);
  1257. oPara.Direction = ParameterDirection.Input;
  1258. oCmd.Parameters.Add(oPara);
  1259. }
  1260. }
  1261. }
  1262. #endregion
  1263.  
  1264. #region out参数的建立
  1265.  
  1266. if (OutParaName != null && OutParaValue != null && IType != null)
  1267. {
  1268. //建立in参数
  1269. for (int i = 0; i < OutParaName.Count; i++)
  1270. {
  1271. SqlParameter oPara = new SqlParameter();
  1272.  
  1273. //调用SQLParamHelper的CreateParameterWithValue()方法来生成不同的参数
  1274. if (OutParaName[i] != null)
  1275. {
  1276. oPara = SQLParamHelper.CreateOutParameterWithValue(OutParaName[i].ToString(), IType[i].ToString());
  1277. oPara.Direction = ParameterDirection.Output;
  1278. oCmd.Parameters.Add(oPara);
  1279. }
  1280. }
  1281. }
  1282.  
  1283. #endregion
  1284.  
  1285. SqlConnection oCn = new SqlConnection(DBCnStr);
  1286. try
  1287. {
  1288. oCn.Open();
  1289. }
  1290. catch (Exception oErr)
  1291. {
  1292. //WriteFile(oErr.Message);
  1293. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  1294.  
  1295. return -1;
  1296. }
  1297.  
  1298. oCmd.Connection = oCn;
  1299.  
  1300. //连接数据库和执行存储过程
  1301. try
  1302. {
  1303. //通过SqlDataAdapter来填充Table
  1304.  
  1305. SqlDataAdapter oDp = new SqlDataAdapter(oCmd.CommandText.ToString(), oCn);
  1306.  
  1307. //建立SqlDataAdapter与SqlCommand的连接
  1308. oDp.SelectCommand = oCmd;
  1309. oDp.DeleteCommand = oCmd;
  1310. oDp.UpdateCommand = oCmd;
  1311. oDp.DeleteCommand = oCmd;
  1312.  
  1313. //填充DataTable
  1314. oDp.Fill(oDT);
  1315. #region 取得返回参数的值
  1316.  
  1317. for (int i = 0; i < OutParaValue.Length; i++)
  1318. {
  1319. OutParaValue[i] = oCmd.Parameters[OutParaName[i].ToString()].Value;
  1320. }
  1321.  
  1322. #endregion
  1323. status = 0;
  1324. }
  1325. catch (Exception oErr)
  1326. {
  1327. //WriteFile(oErr.Message);
  1328. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  1329.  
  1330. status = -1;
  1331. }
  1332. finally
  1333. {
  1334. if (oCn.State == System.Data.ConnectionState.Open)
  1335. {
  1336. oCn.Close();
  1337. }
  1338. oCmd = null;
  1339. }
  1340. return status;
  1341. }
  1342. #endregion
  1343.  
  1344. #region CallStoreProc 调用系统存储过程并影响生成一个DataSet对象
  1345. /// <summary>
  1346. /// 调用系统存储过程并影响生成一个DataSet对象
  1347. /// </summary>
  1348. /// <param name = "strSysSPName">“存储过程枚举类型”</param>
  1349. /// <param name="InParaName">"in参数名字"</param>
  1350. /// <param name = "InParamValue">“in参数列表”</param>
  1351. /// <param name="OutParaName">"out参数名字"</param>
  1352. /// <param name="OutParaValue">"out参数值"</param>
  1353. /// <param name="IType">"out参数的类型"</param>
  1354. /// <param name=" oDs">"引用的DataSet"</param>
  1355. /// <returns>成功则返回1,否则返回-1或错误代码</returns>
  1356. public int CallStoreProc(string strSysSPName, IList InParaName, IList InParamValue, IList OutParaName, ref object[] OutParaValue, DBTYPE[] IType, ref DataSet oDs)
  1357. {
  1358. SqlCommand oCmd = new SqlCommand();
  1359.  
  1360. oCmd.CommandText = strSysSPName;
  1361. oCmd.CommandType = CommandType.StoredProcedure;
  1362.  
  1363. int status = -1;
  1364.  
  1365. #region in参数的建立
  1366. if (InParamValue != null && InParaName != null)
  1367. {
  1368. //建立in参数
  1369. for (int i = 0; i < InParamValue.Count; i++)
  1370. {
  1371. SqlParameter oPara = new SqlParameter();
  1372.  
  1373. //调用SQLParamHelper的CreateParameterWithValue()方法来生成不同的参数
  1374. if (InParaName[i] != null)
  1375. {
  1376. oPara = SQLParamHelper.CreateParameterWithValue(InParaName[i].ToString(), InParamValue[i]);
  1377. oPara.Direction = ParameterDirection.Input;
  1378. oCmd.Parameters.Add(oPara);
  1379. }
  1380. }
  1381. }
  1382. #endregion
  1383.  
  1384. #region out参数的建立
  1385.  
  1386. if (OutParaName != null && OutParaValue != null && IType != null)
  1387. {
  1388. //建立in参数
  1389. for (int i = 0; i < OutParaName.Count; i++)
  1390. {
  1391. SqlParameter oPara = new SqlParameter();
  1392.  
  1393. //调用SQLParamHelper的CreateParameterWithValue()方法来生成不同的参数
  1394. if (OutParaName[i] != null)
  1395. {
  1396. oPara = SQLParamHelper.CreateOutParameterWithValue(OutParaName[i].ToString(), IType[i].ToString());
  1397. oPara.Direction = ParameterDirection.Output;
  1398. oCmd.Parameters.Add(oPara);
  1399. }
  1400. }
  1401. }
  1402.  
  1403. #endregion
  1404.  
  1405. SqlConnection oCn = new SqlConnection(DBCnStr);
  1406. try
  1407. {
  1408. oCn.Open();
  1409. }
  1410. catch (Exception oErr)
  1411. {
  1412. //WriteFile(oErr.Message);
  1413. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  1414.  
  1415. return -1;
  1416. }
  1417.  
  1418. oCmd.Connection = oCn;
  1419.  
  1420. //连接数据库和执行存储过程
  1421. try
  1422. {
  1423. //通过SqlDataAdapter来填充Table
  1424. SqlDataAdapter oDp = new SqlDataAdapter(oCmd.CommandText.ToString(), oCn);
  1425.  
  1426. //建立SqlDataAdapter与SqlCommand的连接
  1427. oDp.SelectCommand = oCmd;
  1428. oDp.DeleteCommand = oCmd;
  1429. oDp.UpdateCommand = oCmd;
  1430. oDp.DeleteCommand = oCmd;
  1431. //
  1432. oDp.Fill(oDs);
  1433. #region 取得返回参数的值
  1434. for (int i = 0; i < OutParaValue.Length; i++)
  1435. {
  1436. OutParaValue[i] = oCmd.Parameters[OutParaName[i].ToString()].Value;
  1437. }
  1438. #endregion
  1439. status = 0;
  1440. }
  1441. catch (Exception oErr)
  1442. {
  1443.  
  1444. // WriteFile(oErr.Message);
  1445. Errlog.AppLog(oErr.Message, ErrorType.MviDataBase);
  1446.  
  1447. status = -1;
  1448. }
  1449. finally
  1450. {
  1451. if (oCn.State == System.Data.ConnectionState.Open)
  1452. {
  1453. oCn.Close();
  1454. }
  1455. oCmd = null;
  1456. }
  1457. return status;
  1458. }
  1459. #endregion
  1460.  
  1461. #region GetSqlWhere 产生SQL语句
  1462. /// <summary>
  1463. /// 产生SQL语句
  1464. /// </summary>
  1465. /// <param name="InName">表字段名</param>
  1466. /// <param name="InValue">表字段值</param>
  1467. /// <returns>结果SQL语句</returns>
  1468. public string GetSqlWhere(ArrayList InName, ArrayList InValue)
  1469. {
  1470. DataTable DataTableTmp = new DataTable();
  1471. string StrSqlWhereTmp = "";
  1472. string StrTmp = "";
  1473. string StrName = "";
  1474. string StrValue = "";
  1475.  
  1476. if (InName == null || InValue == null)
  1477. {
  1478. return null;
  1479. }
  1480. for (int i = 0; i < InName.Count; i++)
  1481. {
  1482. StrTmp = InName[i].ToString();
  1483. if (StrTmp.Substring(0, 2) == "#S") //开始时间
  1484. {
  1485. StrName = StrTmp.Substring(2) + " >= ";
  1486. StrValue = "to_date('" + InValue[i].ToString() + "','yyyy-mm-dd HH24:Mi:ss')";
  1487. }
  1488. else if (StrTmp.Substring(0, 2) == "#E")//结束时间
  1489. {
  1490. StrName = StrTmp.Substring(2) + " < ";
  1491. StrValue = "to_date('" + InValue[i].ToString() + "','yyyy-mm-dd HH24:Mi:ss')";
  1492. }
  1493. else if (StrTmp.Substring(0, 2) == "#N")//<>条件
  1494. {
  1495. StrName = StrTmp.Substring(2) + " <> ";
  1496. StrValue = InValue[i].ToString();
  1497. }
  1498. else if (StrTmp.Substring(0, 2) == "#D")//大于条件
  1499. {
  1500. StrName = StrTmp.Substring(2) + ">";
  1501. StrValue = InValue[i].ToString();
  1502. }
  1503. else if (StrTmp.Substring(0, 2) == "#X")//小于条件
  1504. {
  1505. StrName = StrTmp.Substring(2) + "<";
  1506. StrValue = InValue[i].ToString();
  1507. }
  1508. else if (StrTmp.Substring(0, 2) == "#I")//IN条件
  1509. {
  1510. StrName = StrTmp.Substring(2) + " IN (";
  1511. StrValue = InValue[i].ToString() + ")";
  1512. }
  1513. else if (StrTmp.Substring(0, 2) == "#0")//没有条件
  1514. {
  1515. return InValue[i].ToString();
  1516. }
  1517. else //等于条件
  1518. {
  1519. StrName = StrTmp + "=";
  1520. StrValue = InValue[i].ToString();
  1521. }
  1522.  
  1523. StrSqlWhereTmp = StrSqlWhereTmp + StrName + StrValue + " and ";
  1524. }
  1525.  
  1526. StrSqlWhereTmp = StrSqlWhereTmp.Substring(0, StrSqlWhereTmp.Length - 5);
  1527. return StrSqlWhereTmp;
  1528. }
  1529. #endregion
  1530. }
  1531. #endregion
  1532.  
  1533. #region class SQLParamHelper
  1534. /// <summary>
  1535. /// SQLParamHelper
  1536. /// </summary>
  1537. internal class SQLParamHelper
  1538. {
  1539. #region 创建出入参数
  1540. /// <summary>
  1541. /// 根据输入的OBJECT对象生成不同的参数
  1542. /// </summary>
  1543. /// <param name="name">“参数名字”</param>
  1544. /// <param name="nValue">“参数值”</param>
  1545. /// <returns></returns>
  1546. public static SqlParameter CreateParameterWithValue(string name, object nValue)
  1547. {
  1548. string strType;
  1549. SqlParameter param;
  1550. int intLenth = 0;
  1551.  
  1552. if (nValue != null)
  1553. {
  1554. strType = nValue.GetType().ToString();
  1555. intLenth = nValue.ToString().Trim().Length;
  1556. if (intLenth > 0)
  1557. {
  1558. switch (strType)
  1559. {
  1560. case "System.Int32":
  1561. {
  1562. param = new SqlParameter(name, SqlDbType.BigInt, intLenth);
  1563. param.Direction = ParameterDirection.Input;
  1564. param.Value = nValue;
  1565. break;
  1566. }
  1567. case "System.Double":
  1568. {
  1569. param = new SqlParameter(name, SqlDbType.Decimal);
  1570. param.Direction = ParameterDirection.Input;
  1571. param.Value = nValue;
  1572. break;
  1573. }
  1574. case "System.Single":
  1575. {
  1576. param = new SqlParameter(name, SqlDbType.Float);
  1577. param.Direction = ParameterDirection.Input;
  1578. param.Value = nValue;
  1579. break;
  1580. }
  1581. case "System.UInt64":
  1582. {
  1583. param = new SqlParameter(name, SqlDbType.BigInt, intLenth);
  1584. param.Direction = ParameterDirection.Input;
  1585. param.Value = nValue;
  1586. break;
  1587. }
  1588. case "System.Int64":
  1589. {
  1590. param = new SqlParameter(name, SqlDbType.BigInt, intLenth);
  1591. param.Direction = ParameterDirection.Input;
  1592. param.Value = nValue;
  1593. break;
  1594. }
  1595. case "System.Decimal":
  1596. {
  1597. param = new SqlParameter(name, SqlDbType.Decimal, intLenth);
  1598. param.Direction = ParameterDirection.Input;
  1599. param.Value = nValue;
  1600. break;
  1601. }
  1602. case "System.Object":
  1603. {
  1604. param = new SqlParameter(name, SqlDbType.Real, intLenth);
  1605. param.Direction = ParameterDirection.Input;
  1606. param.Value = nValue;
  1607. break;
  1608. }
  1609. case "System.UInt16":
  1610. {
  1611. param = new SqlParameter(name, SqlDbType.BigInt, intLenth);
  1612. param.Direction = ParameterDirection.Input;
  1613. param.Value = nValue;
  1614. break;
  1615. }
  1616. case "System.Int16":
  1617. {
  1618. param = new SqlParameter(name, SqlDbType.BigInt, intLenth);
  1619. param.Direction = ParameterDirection.Input;
  1620. param.Value = nValue;
  1621. break;
  1622. }
  1623. case "System.Boolean":
  1624. {
  1625. param = new SqlParameter(name, SqlDbType.Binary);
  1626. param.Direction = ParameterDirection.Input;
  1627. bool bolTemp = (bool)nValue;
  1628. param.Value = (bolTemp == true ? 1 : 0);
  1629. break;
  1630. }
  1631. case "System.String":
  1632. {
  1633. param = new SqlParameter(name, SqlDbType.VarChar, intLenth);
  1634. param.Direction = ParameterDirection.Input;
  1635. param.Value = nValue;
  1636. break;
  1637. }
  1638. case "System.DateTime":
  1639. {
  1640. param = new SqlParameter(name, SqlDbType.DateTime, intLenth);
  1641. param.Direction = ParameterDirection.Input;
  1642. param.Value = nValue;
  1643. break;
  1644. }
  1645. case "System.Char":
  1646. {
  1647. param = new SqlParameter(name, SqlDbType.Char, intLenth);
  1648. param.Direction = ParameterDirection.Input;
  1649. param.Value = nValue;
  1650. break;
  1651. }
  1652. case "System.SByte":
  1653. {
  1654. param = new SqlParameter(name, SqlDbType.Bit, intLenth);
  1655. param.Direction = ParameterDirection.Input;
  1656. param.Value = nValue;
  1657. break;
  1658. }
  1659. case "System_XMl":
  1660. {
  1661. param = new SqlParameter(name, SqlDbType.Xml, 300);
  1662. param.Direction = ParameterDirection.Input;
  1663. param.Value = nValue;
  1664. break;
  1665. }
  1666. case "System.Text":
  1667. {
  1668. if (intLenth < 2000)
  1669. {
  1670. param = new SqlParameter(name, SqlDbType.Text, intLenth);
  1671. }
  1672. else
  1673. {
  1674. param = new SqlParameter(name, SqlDbType.Text);
  1675. }
  1676. param.Direction = ParameterDirection.Input;
  1677. param.Value = nValue;
  1678. break;
  1679. }
  1680. default:
  1681. {
  1682. param = new SqlParameter(name, SqlDbType.Variant);
  1683. param.Direction = ParameterDirection.Input;
  1684. param.Value = nValue;
  1685. break;
  1686. }
  1687. }
  1688. param.Direction = ParameterDirection.Input;
  1689. }
  1690. else
  1691. {
  1692. param = new SqlParameter(name, SqlDbType.VarChar, 10);
  1693. param.Direction = ParameterDirection.Input;
  1694. param.Value = "";
  1695. }
  1696. }
  1697. else
  1698. {
  1699. param = new SqlParameter(name, SqlDbType.Variant);
  1700. param.Direction = ParameterDirection.Input;
  1701. param.Value = null;
  1702. }
  1703. return param;
  1704.  
  1705. }
  1706. #endregion
  1707.  
  1708. #region CreateOutParameterWithValue 建立输出参数
  1709. /// <summary>
  1710. /// 建立输出参数
  1711. /// </summary>
  1712. /// <param name="name">"参数名"</param>
  1713. /// <param name="objType">"参数类型"</param>
  1714. /// <returns></returns>
  1715. public static SqlParameter CreateOutParameterWithValue(string name, string objType)
  1716. {
  1717. string strType = objType;
  1718.  
  1719. SqlParameter param;
  1720.  
  1721. switch (strType)
  1722. {
  1723. case "System_Object":
  1724. {
  1725. param = new SqlParameter(name, SqlDbType.Variant);
  1726. param.Direction = ParameterDirection.Output;
  1727. break;
  1728. }
  1729. case "System_Single":
  1730. {
  1731. param = new SqlParameter(name, SqlDbType.Float);
  1732. param.Direction = ParameterDirection.Output;
  1733. break;
  1734. }
  1735. case "System_UInt64":
  1736. {
  1737. param = new SqlParameter(name, SqlDbType.BigInt);
  1738. param.Direction = ParameterDirection.Output;
  1739. break;
  1740. }
  1741. case "System_Int64":
  1742. {
  1743. param = new SqlParameter(name, SqlDbType.BigInt);
  1744. param.Direction = ParameterDirection.Output;
  1745. break;
  1746. }
  1747. case "System_Int32":
  1748. {
  1749. param = new SqlParameter(name, SqlDbType.Int);
  1750. param.Direction = ParameterDirection.Output;
  1751. break;
  1752. }
  1753. case "System_UInt16":
  1754. {
  1755. param = new SqlParameter(name, SqlDbType.SmallInt);
  1756. param.Direction = ParameterDirection.Output;
  1757. break;
  1758. }
  1759. case "System_Int16":
  1760. {
  1761. param = new SqlParameter(name, SqlDbType.SmallInt);
  1762. param.Direction = ParameterDirection.Output;
  1763. break;
  1764. }
  1765. case "System_Double":
  1766. {
  1767. param = new SqlParameter(name, SqlDbType.Float);
  1768. param.Direction = ParameterDirection.Output;
  1769. break;
  1770. }
  1771. case "System_Decimal":
  1772. {
  1773. param = new SqlParameter(name, SqlDbType.Decimal);
  1774. param.Direction = ParameterDirection.Output;
  1775. break;
  1776. }
  1777. case "System_Boolean":
  1778. {
  1779. param = new SqlParameter(name, SqlDbType.Binary);
  1780. param.Direction = ParameterDirection.Output;
  1781. break;
  1782. }
  1783. case "System_String":
  1784. {
  1785. param = new SqlParameter(name, SqlDbType.VarChar, 200);
  1786. param.Direction = ParameterDirection.Output;
  1787. break;
  1788. }
  1789. case "System_DateTime":
  1790. {
  1791. param = new SqlParameter(name, SqlDbType.DateTime);
  1792. param.Direction = ParameterDirection.Output;
  1793. break;
  1794. }
  1795. case "System_Char":
  1796. {
  1797. param = new SqlParameter(name, SqlDbType.Char, 100);
  1798. param.Direction = ParameterDirection.Output;
  1799. break;
  1800. }
  1801. case "System_SByte":
  1802. {
  1803. param = new SqlParameter(name, SqlDbType.NChar, 30);
  1804. param.Direction = ParameterDirection.Output;
  1805. break;
  1806. }
  1807. case "System_Text":
  1808. {
  1809. param = new SqlParameter(name, SqlDbType.Text, 300);
  1810. param.Direction = ParameterDirection.Output;
  1811. break;
  1812. }
  1813. case "System_XMl":
  1814. {
  1815. param = new SqlParameter(name, SqlDbType.Xml, 300);
  1816. param.Direction = ParameterDirection.Output;
  1817. break;
  1818. }
  1819. default:
  1820. {
  1821. param = new SqlParameter(name, SqlDbType.Variant);
  1822. param.Direction = ParameterDirection.Output;
  1823. break;
  1824. }
  1825. }
  1826. return param;
  1827.  
  1828. }
  1829. #endregion CreateOutParams
  1830.  
  1831. #region CreateParameter 创建形式参数
  1832. /// <summary>
  1833. /// 转换参数为SQL语句的表达式
  1834. /// </summary>
  1835. /// <param name="nValue">传入的Object类型值</param>
  1836. /// <returns>已经转换好的String</returns>
  1837. public static string CreateParameter(SqlParameter oValue)
  1838. {
  1839. string strTemPara;
  1840. object oPara_Value = oValue.Value;
  1841.  
  1842. if (oPara_Value != null)
  1843. {
  1844. string strType = oValue.SqlDbType.ToString();
  1845. switch (strType)
  1846. {
  1847. case "VarChar":
  1848. {
  1849. strTemPara = "'" + CheckMark(oPara_Value) + "'";
  1850. break;
  1851. }
  1852. case "Char":
  1853. {
  1854. strTemPara = "'" + CheckMark(oPara_Value) + "'";
  1855. break;
  1856. }
  1857. case "NChar":
  1858. {
  1859. strTemPara = "'" + CheckMark(oPara_Value) + "'";
  1860. break;
  1861. }
  1862. case "NVarChar":
  1863. {
  1864. strTemPara = "'" + CheckMark(oPara_Value) + "'";
  1865. break;
  1866. }
  1867. //日期型
  1868. case "DateTime":
  1869. {
  1870. DateTime dt = new DateTime();
  1871. dt = (DateTime)oPara_Value;
  1872.  
  1873. string strTP = "'" + dt.Year + "-" + dt.Month + "-" + dt.Day;
  1874. strTP += " " + dt.Hour.ToString() + ":" + dt.Minute.ToString();
  1875. strTP += ":" + dt.Second.ToString() + "',";
  1876. strTemPara = "TO_DATE(" + strTP + "'yyyy-mm-dd hh24:mi:ss'" + ")";
  1877. break;
  1878. }
  1879. case "LongVarChar":
  1880. {
  1881. strTemPara = "'" + CheckMark(oPara_Value) + "'";
  1882. break;
  1883. }
  1884. case "Clob":
  1885. {
  1886. strTemPara = "'" + CheckMark(oPara_Value) + "'";
  1887. break;
  1888. }
  1889. default:
  1890. {
  1891. strTemPara = oPara_Value.ToString();
  1892. break;
  1893. }
  1894. }
  1895. }
  1896. else
  1897. {
  1898. //将null传入
  1899. strTemPara = "null";
  1900. }
  1901. return strTemPara;
  1902. }
  1903. #endregion
  1904.  
  1905. #region CheckMark 替换object的'为''并转换为String
  1906. /// <summary>
  1907. /// 替换object的'为''并转换为String
  1908. /// </summary>
  1909. /// <param name="objIn">传入的Object类型</param>
  1910. /// <returns>已经替换'为''的String</returns>
  1911. private static string CheckMark(object objIn)
  1912. {
  1913. string strTmp = objIn.ToString();
  1914.  
  1915. return strTmp.Replace("'", "''"); // modified by apenni 06.01.02
  1916.  
  1917. //string strRet = "";
  1918. //for (int i = 0; i < strTmp.Length; i++)
  1919. //{
  1920. // if (strTmp[i].ToString() == "'")
  1921. // {
  1922. // strRet += "''";
  1923. // }
  1924. // else
  1925. // {
  1926. // strRet += strTmp[i].ToString();
  1927. // }
  1928. //}
  1929. //return strRet;
  1930. }
  1931. #endregion
  1932. }
  1933. #endregion

ASP.NET经典的、封装好的ADO.NET类包的更多相关文章

  1. ASP.NET 经典60道面试题

    转:http://bbs.chinaunix.net/thread-4065577-1-1.html ASP.NET 经典60道面试题 1. 简述 private. protected. public ...

  2. 在ASP.NET 5项目中使用和调试外部源代码包

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于在ASP.NET 5中,项目依赖都是通过"包"来引用,所以使用 ...

  3. iOS开发拓展篇—封装音频文件播放工具类

    iOS开发拓展篇—封装音频文件播放工具类 一.简单说明 1.关于音乐播放的简单说明 (1)音乐播放用到一个叫做AVAudioPlayer的类 (2)AVAudioPlayer常用方法 加载音乐文件 - ...

  4. JBPM4入门——4.封装流程管理的工具类(JbpmUtil)

    本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...

  5. c# 封装的文件夹操作类之复制文件夹

    c#  封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs /// <summary> /// 文件夹操作类 /// ...

  6. Redis进阶实践之九 独立封装的RedisClient客户端工具类(转载9)

    Redis进阶实践之九 独立封装的RedisClient客户端工具类 一.引言 今天开始有关Redis学习的第九篇文章了,以后肯定会大量系统使用Redis作为缓存介质,为了更好的更好的Redis,自己 ...

  7. 封装php redis缓存操作类

    封装php redis缓存操作类,集成了连接redis并判断连接是否成功,redis数据库选择,检测redis键是否存在,获取值,写入值,设置生存时间和删除清空操作. php redis类代码: &l ...

  8. python+selenium之自定义封装一个简单的Log类

    python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...

  9. 基于AFNetworking封装的网络请求工具类【原创】

    今天给大家共享一个我自己封装的网络请求类,希望能帮助到大家. 前提,导入AFNetworking框架, 关于修改AFN源码:通常序列化时做对text/plan等的支持时,可以一劳永逸的修改源代码,在a ...

随机推荐

  1. Python作业之用户管理

    作业 流程图没有画,懒,不想画 readme没有写,懒,不想写.看注释吧233333 #! /usr/bin/env python # -*- coding: utf-8 -*- # __author ...

  2. CyclicBarrier及CountDownLatch的使用

    CountDownLatch位于java.util.concurrent包下,是JDK1.5的并发包下的新特性. 首先根据Oracle的官方文档看看CountDownLatch的定义: A synch ...

  3. numpy.argmax 用在求解混淆矩阵用

    numpy.argmax numpy.argmax(a, axis=None, out=None)[source] Returns the indices of the maximum values ...

  4. [原创]Java开发如何在线打开Word文件

    此方案使用了PageOffice产品实现在线打开Word文档: 1. 首先从PageOffice官网下载产品开发包,http://www.zhuozhengsoft.com/dowm/ ,下载Page ...

  5. SSD Network Architecture--keras version

    这里的网络架构和论文中插图中的网络架构是相一致的.对了,忘了说了,这里使用的keras版本是1.2.2,等源码读完之后,我自己改一个2.0.6版本上传到github上面.可别直接粘贴复制,里面有些中文 ...

  6. <十三>UML核心视图静态视图之业务用例图

    一:uml的核心视图 --->如果说UML是一门语言,上一章学习的参与者等元素是uml的基本词汇,那么视图就是语法.uml通过视图将基元素组织在一起,形成有意义的句子. --->uml可视 ...

  7. HiddenHttpMethodFilter

    操作步骤: 在web.xml中配置: 删除操作: 其他操作即为将DELETE换成INPUT/POST/GET

  8. JQ对象和原生DOM对象

    相同点:两者本质上都是DOM元素. 不同点:JQ对象是在原生DOM对象上进行了一次封装,使开发人员使用起来更简洁.高效. 两者之间用法也完全不同,很说初学者经常混淆. 其实区分两者并不难, 1.语法不 ...

  9. MTK UART串口调试

    一.UART初始化 1. kernel-3.18/drivers/misc/mediatek/uart/uart.c static int __init mtk_uart_init(void) { ; ...

  10. bzoj3456

    分治+ntt 设dp[i]表示i个点的图联通的方案数 那么考虑dp,利用容斥,总-不符合,枚举j=1->i-1,然后考虑不符合,那么考虑和1联通的连通块,剩下的不和1连通,那么dp[i]=2^t ...