DbHelperSQL.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Configuration;
  7. using System.Data.Common;
  8. using System.Collections.Generic;
  9. namespace Com.Jpsoft.Hospital.DBUtility
  10. {
  11. /// <summary>
  12. /// 数据访问抽象基础类
  13. /// Copyright (C) 2004-2008 By LiTianPing
  14. /// </summary>
  15. public abstract class DbHelperSQL
  16. {
  17. //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
  18. public static string connectionString = ConfigurationManager.ConnectionStrings["conn"].ToString();
  19. public DbHelperSQL()
  20. {
  21. }
  22. #region 公用方法
  23. /// <summary>
  24. /// 判断是否存在某表的某个字段
  25. /// </summary>
  26. /// <param name="tableName">表名称</param>
  27. /// <param name="columnName">列名称</param>
  28. /// <returns>是否存在</returns>
  29. public static bool ColumnExists(string tableName, string columnName)
  30. {
  31. string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'";
  32. object res = GetSingle(sql);
  33. if (res == null)
  34. {
  35. return false;
  36. }
  37. return Convert.ToInt32(res) > 0;
  38. }
  39. public static int GetMaxID(string FieldName, string TableName)
  40. {
  41. string strsql = "select max(" + FieldName + ")+1 from " + TableName;
  42. object obj = DbHelperSQL.GetSingle(strsql);
  43. if (obj == null)
  44. {
  45. return 1;
  46. }
  47. else
  48. {
  49. return int.Parse(obj.ToString());
  50. }
  51. }
  52. public static bool Exists(string strSql)
  53. {
  54. object obj = DbHelperSQL.GetSingle(strSql);
  55. int cmdresult;
  56. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  57. {
  58. cmdresult = 0;
  59. }
  60. else
  61. {
  62. cmdresult = int.Parse(obj.ToString());
  63. }
  64. if (cmdresult == 0)
  65. {
  66. return false;
  67. }
  68. else
  69. {
  70. return true;
  71. }
  72. }
  73. /// <summary>
  74. /// 表是否存在
  75. /// </summary>
  76. /// <param name="TableName"></param>
  77. /// <returns></returns>
  78. public static bool TabExists(string TableName)
  79. {
  80. string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
  81. //string strsql = "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')";
  82. object obj = DbHelperSQL.GetSingle(strsql);
  83. int cmdresult;
  84. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  85. {
  86. cmdresult = 0;
  87. }
  88. else
  89. {
  90. cmdresult = int.Parse(obj.ToString());
  91. }
  92. if (cmdresult == 0)
  93. {
  94. return false;
  95. }
  96. else
  97. {
  98. return true;
  99. }
  100. }
  101. public static bool Exists(string strSql, params SqlParameter[] cmdParms)
  102. {
  103. object obj = DbHelperSQL.GetSingle(strSql, cmdParms);
  104. int cmdresult;
  105. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  106. {
  107. cmdresult = 0;
  108. }
  109. else
  110. {
  111. cmdresult = int.Parse(obj.ToString());
  112. }
  113. if (cmdresult == 0)
  114. {
  115. return false;
  116. }
  117. else
  118. {
  119. return true;
  120. }
  121. }
  122. #endregion
  123. #region 执行简单SQL语句
  124. /// <summary>
  125. /// 执行SQL语句,返回影响的记录数
  126. /// </summary>
  127. /// <param name="SQLString">SQL语句</param>
  128. /// <returns>影响的记录数</returns>
  129. public static int ExecuteSql(string SQLString)
  130. {
  131. using (SqlConnection connection = new SqlConnection(connectionString))
  132. {
  133. using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  134. {
  135. try
  136. {
  137. connection.Open();
  138. int rows = cmd.ExecuteNonQuery();
  139. return rows;
  140. }
  141. catch (System.Data.SqlClient.SqlException e)
  142. {
  143. connection.Close();
  144. throw e;
  145. }
  146. }
  147. }
  148. }
  149. public static int ExecuteSqlByTime(string SQLString, int Times)
  150. {
  151. using (SqlConnection connection = new SqlConnection(connectionString))
  152. {
  153. using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  154. {
  155. try
  156. {
  157. connection.Open();
  158. cmd.CommandTimeout = Times;
  159. int rows = cmd.ExecuteNonQuery();
  160. return rows;
  161. }
  162. catch (System.Data.SqlClient.SqlException e)
  163. {
  164. connection.Close();
  165. throw e;
  166. }
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 执行多条SQL语句,实现数据库事务。
  172. /// </summary>
  173. /// <param name="SQLStringList">多条SQL语句</param>
  174. public static int ExecuteSqlTran(List<String> SQLStringList)
  175. {
  176. using (SqlConnection conn = new SqlConnection(connectionString))
  177. {
  178. conn.Open();
  179. SqlCommand cmd = new SqlCommand();
  180. cmd.Connection = conn;
  181. SqlTransaction tx = conn.BeginTransaction();
  182. cmd.Transaction = tx;
  183. try
  184. {
  185. int count = 0;
  186. for (int n = 0; n < SQLStringList.Count; n++)
  187. {
  188. string strsql = SQLStringList[n];
  189. if (strsql.Trim().Length > 1)
  190. {
  191. cmd.CommandText = strsql;
  192. count += cmd.ExecuteNonQuery();
  193. }
  194. }
  195. tx.Commit();
  196. return count;
  197. }
  198. catch
  199. {
  200. tx.Rollback();
  201. return 0;
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// 执行带一个存储过程参数的的SQL语句。
  207. /// </summary>
  208. /// <param name="SQLString">SQL语句</param>
  209. /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  210. /// <returns>影响的记录数</returns>
  211. public static int ExecuteSql(string SQLString, string content)
  212. {
  213. using (SqlConnection connection = new SqlConnection(connectionString))
  214. {
  215. SqlCommand cmd = new SqlCommand(SQLString, connection);
  216. System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
  217. myParameter.Value = content;
  218. cmd.Parameters.Add(myParameter);
  219. try
  220. {
  221. connection.Open();
  222. int rows = cmd.ExecuteNonQuery();
  223. return rows;
  224. }
  225. catch (System.Data.SqlClient.SqlException e)
  226. {
  227. throw e;
  228. }
  229. finally
  230. {
  231. cmd.Dispose();
  232. connection.Close();
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// 执行带一个存储过程参数的的SQL语句。
  238. /// </summary>
  239. /// <param name="SQLString">SQL语句</param>
  240. /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  241. /// <returns>影响的记录数</returns>
  242. public static object ExecuteSqlGet(string SQLString, string content)
  243. {
  244. using (SqlConnection connection = new SqlConnection(connectionString))
  245. {
  246. SqlCommand cmd = new SqlCommand(SQLString, connection);
  247. System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
  248. myParameter.Value = content;
  249. cmd.Parameters.Add(myParameter);
  250. try
  251. {
  252. connection.Open();
  253. object obj = cmd.ExecuteScalar();
  254. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  255. {
  256. return null;
  257. }
  258. else
  259. {
  260. return obj;
  261. }
  262. }
  263. catch (System.Data.SqlClient.SqlException e)
  264. {
  265. throw e;
  266. }
  267. finally
  268. {
  269. cmd.Dispose();
  270. connection.Close();
  271. }
  272. }
  273. }
  274. /// <summary>
  275. /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
  276. /// </summary>
  277. /// <param name="strSQL">SQL语句</param>
  278. /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
  279. /// <returns>影响的记录数</returns>
  280. public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
  281. {
  282. using (SqlConnection connection = new SqlConnection(connectionString))
  283. {
  284. SqlCommand cmd = new SqlCommand(strSQL, connection);
  285. System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);
  286. myParameter.Value = fs;
  287. cmd.Parameters.Add(myParameter);
  288. try
  289. {
  290. connection.Open();
  291. int rows = cmd.ExecuteNonQuery();
  292. return rows;
  293. }
  294. catch (System.Data.SqlClient.SqlException e)
  295. {
  296. throw e;
  297. }
  298. finally
  299. {
  300. cmd.Dispose();
  301. connection.Close();
  302. }
  303. }
  304. }
  305. /// <summary>
  306. /// 执行一条计算查询结果语句,返回查询结果(object)。
  307. /// </summary>
  308. /// <param name="SQLString">计算查询结果语句</param>
  309. /// <returns>查询结果(object)</returns>
  310. public static object GetSingle(string SQLString)
  311. {
  312. using (SqlConnection connection = new SqlConnection(connectionString))
  313. {
  314. using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  315. {
  316. try
  317. {
  318. connection.Open();
  319. object obj = cmd.ExecuteScalar();
  320. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  321. {
  322. return null;
  323. }
  324. else
  325. {
  326. return obj;
  327. }
  328. }
  329. catch (System.Data.SqlClient.SqlException e)
  330. {
  331. connection.Close();
  332. throw e;
  333. }
  334. }
  335. }
  336. }
  337. public static object GetSingle(string SQLString, int Times)
  338. {
  339. using (SqlConnection connection = new SqlConnection(connectionString))
  340. {
  341. using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  342. {
  343. try
  344. {
  345. connection.Open();
  346. cmd.CommandTimeout = Times;
  347. object obj = cmd.ExecuteScalar();
  348. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  349. {
  350. return null;
  351. }
  352. else
  353. {
  354. return obj;
  355. }
  356. }
  357. catch (System.Data.SqlClient.SqlException e)
  358. {
  359. connection.Close();
  360. throw e;
  361. }
  362. }
  363. }
  364. }
  365. /// <summary>
  366. /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
  367. /// </summary>
  368. /// <param name="strSQL">查询语句</param>
  369. /// <returns>SqlDataReader</returns>
  370. public static SqlDataReader ExecuteReader(string strSQL)
  371. {
  372. SqlConnection connection = new SqlConnection(connectionString);
  373. SqlCommand cmd = new SqlCommand(strSQL, connection);
  374. try
  375. {
  376. connection.Open();
  377. SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  378. return myReader;
  379. }
  380. catch (System.Data.SqlClient.SqlException e)
  381. {
  382. throw e;
  383. }
  384. }
  385. /// <summary>
  386. /// 执行查询语句,返回DataSet
  387. /// </summary>
  388. /// <param name="SQLString">查询语句</param>
  389. /// <returns>DataSet</returns>
  390. public static DataSet Query(string SQLString)
  391. {
  392. using (SqlConnection connection = new SqlConnection(connectionString))
  393. {
  394. DataSet ds = new DataSet();
  395. try
  396. {
  397. connection.Open();
  398. SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
  399. command.SelectCommand.CommandTimeout = 600;
  400. command.Fill(ds, "ds");
  401. }
  402. catch (System.Data.SqlClient.SqlException ex)
  403. {
  404. throw new Exception(ex.Message);
  405. }
  406. return ds;
  407. }
  408. }
  409. public static DataSet Query(string SQLString, int Times)
  410. {
  411. using (SqlConnection connection = new SqlConnection(connectionString))
  412. {
  413. DataSet ds = new DataSet();
  414. try
  415. {
  416. connection.Open();
  417. SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
  418. command.SelectCommand.CommandTimeout = Times;
  419. command.Fill(ds, "ds");
  420. }
  421. catch (System.Data.SqlClient.SqlException ex)
  422. {
  423. throw new Exception(ex.Message);
  424. }
  425. return ds;
  426. }
  427. }
  428. #endregion
  429. #region 执行带参数的SQL语句
  430. /// <summary>
  431. /// 执行SQL语句,返回影响的记录数
  432. /// </summary>
  433. /// <param name="SQLString">SQL语句</param>
  434. /// <returns>影响的记录数</returns>
  435. public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
  436. {
  437. using (SqlConnection connection = new SqlConnection(connectionString))
  438. {
  439. using (SqlCommand cmd = new SqlCommand())
  440. {
  441. try
  442. {
  443. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  444. int rows = cmd.ExecuteNonQuery();
  445. cmd.Parameters.Clear();
  446. return rows;
  447. }
  448. catch (System.Data.SqlClient.SqlException e)
  449. {
  450. throw e;
  451. }
  452. }
  453. }
  454. }
  455. /// <summary>
  456. /// 执行多条SQL语句,实现数据库事务。
  457. /// </summary>
  458. /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
  459. public static void ExecuteSqlTran(Hashtable SQLStringList)
  460. {
  461. using (SqlConnection conn = new SqlConnection(connectionString))
  462. {
  463. conn.Open();
  464. using (SqlTransaction trans = conn.BeginTransaction())
  465. {
  466. SqlCommand cmd = new SqlCommand();
  467. try
  468. {
  469. //循环
  470. foreach (DictionaryEntry myDE in SQLStringList)
  471. {
  472. string cmdText = myDE.Key.ToString();
  473. SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
  474. PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  475. int val = cmd.ExecuteNonQuery();
  476. cmd.Parameters.Clear();
  477. }
  478. trans.Commit();
  479. }
  480. catch
  481. {
  482. trans.Rollback();
  483. throw;
  484. }
  485. }
  486. }
  487. }
  488. /// <summary>
  489. /// 执行多条SQL语句,实现数据库事务。
  490. /// </summary>
  491. /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
  492. public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
  493. {
  494. using (SqlConnection conn = new SqlConnection(connectionString))
  495. {
  496. conn.Open();
  497. using (SqlTransaction trans = conn.BeginTransaction())
  498. {
  499. SqlCommand cmd = new SqlCommand();
  500. try
  501. {
  502. int indentity = 0;
  503. //循环
  504. foreach (DictionaryEntry myDE in SQLStringList)
  505. {
  506. string cmdText = myDE.Key.ToString();
  507. SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
  508. foreach (SqlParameter q in cmdParms)
  509. {
  510. if (q.Direction == ParameterDirection.InputOutput)
  511. {
  512. q.Value = indentity;
  513. }
  514. }
  515. PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  516. int val = cmd.ExecuteNonQuery();
  517. foreach (SqlParameter q in cmdParms)
  518. {
  519. if (q.Direction == ParameterDirection.Output)
  520. {
  521. indentity = Convert.ToInt32(q.Value);
  522. }
  523. }
  524. cmd.Parameters.Clear();
  525. }
  526. trans.Commit();
  527. }
  528. catch
  529. {
  530. trans.Rollback();
  531. throw;
  532. }
  533. }
  534. }
  535. }
  536. /// <summary>
  537. /// 执行一条计算查询结果语句,返回查询结果(object)。
  538. /// </summary>
  539. /// <param name="SQLString">计算查询结果语句</param>
  540. /// <returns>查询结果(object)</returns>
  541. public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)
  542. {
  543. using (SqlConnection connection = new SqlConnection(connectionString))
  544. {
  545. using (SqlCommand cmd = new SqlCommand())
  546. {
  547. try
  548. {
  549. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  550. object obj = cmd.ExecuteScalar();
  551. cmd.Parameters.Clear();
  552. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  553. {
  554. return null;
  555. }
  556. else
  557. {
  558. return obj;
  559. }
  560. }
  561. catch (System.Data.SqlClient.SqlException e)
  562. {
  563. throw e;
  564. }
  565. }
  566. }
  567. }
  568. /// <summary>
  569. /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
  570. /// </summary>
  571. /// <param name="strSQL">查询语句</param>
  572. /// <returns>SqlDataReader</returns>
  573. public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms)
  574. {
  575. SqlConnection connection = new SqlConnection(connectionString);
  576. SqlCommand cmd = new SqlCommand();
  577. try
  578. {
  579. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  580. SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  581. cmd.Parameters.Clear();
  582. return myReader;
  583. }
  584. catch (System.Data.SqlClient.SqlException e)
  585. {
  586. throw e;
  587. }
  588. finally
  589. {
  590. cmd.Dispose();
  591. connection.Close();
  592. }
  593. }
  594. /// <summary>
  595. /// 执行查询语句,返回DataSet
  596. /// </summary>
  597. /// <param name="SQLString">查询语句</param>
  598. /// <returns>DataSet</returns>
  599. public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)
  600. {
  601. using (SqlConnection connection = new SqlConnection(connectionString))
  602. {
  603. SqlCommand cmd = new SqlCommand();
  604. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  605. using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  606. {
  607. DataSet ds = new DataSet();
  608. try
  609. {
  610. da.Fill(ds, "ds");
  611. cmd.Parameters.Clear();
  612. }
  613. catch (System.Data.SqlClient.SqlException ex)
  614. {
  615. throw new Exception(ex.Message);
  616. }
  617. return ds;
  618. }
  619. }
  620. }
  621. private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
  622. {
  623. if (conn.State != ConnectionState.Open)
  624. conn.Open();
  625. cmd.Connection = conn;
  626. cmd.CommandText = cmdText;
  627. if (trans != null)
  628. cmd.Transaction = trans;
  629. cmd.CommandType = CommandType.Text;//cmdType;
  630. if (cmdParms != null)
  631. {
  632. foreach (SqlParameter parameter in cmdParms)
  633. {
  634. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  635. (parameter.Value == null))
  636. {
  637. parameter.Value = DBNull.Value;
  638. }
  639. else if (parameter.DbType == DbType.DateTime)
  640. {
  641. if (Convert.ToDateTime(parameter.Value) < DateTime.Parse("1900-1-1"))
  642. {
  643. parameter.Value = DBNull.Value;
  644. }
  645. }
  646. cmd.Parameters.Add(parameter);
  647. }
  648. }
  649. }
  650. #endregion
  651. #region 存储过程操作
  652. /// <summary>
  653. /// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
  654. /// </summary>
  655. /// <param name="storedProcName">存储过程名</param>
  656. /// <param name="parameters">存储过程参数</param>
  657. /// <returns>SqlDataReader</returns>
  658. public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
  659. {
  660. SqlConnection connection = new SqlConnection(connectionString);
  661. SqlDataReader returnReader;
  662. connection.Open();
  663. SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
  664. command.CommandType = CommandType.StoredProcedure;
  665. command.CommandTimeout = 180;
  666. returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
  667. return returnReader;
  668. }
  669. /// <summary>
  670. /// 执行存储过程
  671. /// </summary>
  672. /// <param name="storedProcName">存储过程名</param>
  673. /// <param name="parameters">存储过程参数</param>
  674. /// <param name="tableName">DataSet结果中的表名</param>
  675. /// <returns>DataSet</returns>
  676. public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
  677. {
  678. using (SqlConnection connection = new SqlConnection(connectionString))
  679. {
  680. DataSet dataSet = new DataSet();
  681. connection.Open();
  682. SqlDataAdapter sqlDA = new SqlDataAdapter();
  683. sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
  684. sqlDA.Fill(dataSet, tableName);
  685. connection.Close();
  686. return dataSet;
  687. }
  688. }
  689. public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
  690. {
  691. using (SqlConnection connection = new SqlConnection(connectionString))
  692. {
  693. DataSet dataSet = new DataSet();
  694. connection.Open();
  695. SqlDataAdapter sqlDA = new SqlDataAdapter();
  696. sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
  697. sqlDA.SelectCommand.CommandTimeout = Times;
  698. sqlDA.Fill(dataSet, tableName);
  699. connection.Close();
  700. return dataSet;
  701. }
  702. }
  703. /// <summary>
  704. /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
  705. /// </summary>
  706. /// <param name="connection">数据库连接</param>
  707. /// <param name="storedProcName">存储过程名</param>
  708. /// <param name="parameters">存储过程参数</param>
  709. /// <returns>SqlCommand</returns>
  710. private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
  711. {
  712. SqlCommand command = new SqlCommand(storedProcName, connection);
  713. command.CommandType = CommandType.StoredProcedure;
  714. foreach (SqlParameter parameter in parameters)
  715. {
  716. if (parameter != null)
  717. {
  718. // 检查未分配值的输出参数,将其分配以DBNull.Value.
  719. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  720. (parameter.Value == null))
  721. {
  722. parameter.Value = DBNull.Value;
  723. }
  724. command.Parameters.Add(parameter);
  725. }
  726. }
  727. return command;
  728. }
  729. /// <summary>
  730. /// 执行存储过程,返回影响的行数
  731. /// </summary>
  732. /// <param name="storedProcName">存储过程名</param>
  733. /// <param name="parameters">存储过程参数</param>
  734. /// <param name="rowsAffected">影响的行数</param>
  735. /// <returns></returns>
  736. public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
  737. {
  738. using (SqlConnection connection = new SqlConnection(connectionString))
  739. {
  740. int result;
  741. connection.Open();
  742. SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
  743. rowsAffected = command.ExecuteNonQuery();
  744. result = (int)command.Parameters["ReturnValue"].Value;
  745. //Connection.Close();
  746. return result;
  747. }
  748. }
  749. /// <summary>
  750. /// 创建 SqlCommand 对象实例(用来返回一个整数值)
  751. /// </summary>
  752. /// <param name="storedProcName">存储过程名</param>
  753. /// <param name="parameters">存储过程参数</param>
  754. /// <returns>SqlCommand 对象实例</returns>
  755. private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
  756. {
  757. SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
  758. command.Parameters.Add(new SqlParameter("ReturnValue",
  759. SqlDbType.Int, 4, ParameterDirection.ReturnValue,
  760. false, 0, 0, string.Empty, DataRowVersion.Default, null));
  761. return command;
  762. }
  763. #endregion
  764. }
  765. }