Geen omschrijving

DbHelperSQL.cs 31KB

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