DbHelperSQL.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Collections.Generic;
  6. namespace PMSUI.Model
  7. {
  8. /// <summary>
  9. /// 数据访问抽象基础类
  10. /// Copyright (C) Maticsoft
  11. /// </summary>
  12. //public abstract class DbHelperSQL
  13. //{
  14. // //数据库连接字符串(config来配置),多数据库可使用DbHelperSQLP来实现.
  15. // //public static string connectionString = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
  16. // //public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["myconn"].ToString();
  17. // //public static string connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
  18. // //public static string connectionString = ConfigurationSettings.AppSettings["myconn"];
  19. // //public static string KWip = "192.168.1.90";
  20. // //public static string KWsqlname = "WMS_HXGD";
  21. // //public static string AGVSIP = "192.168.1.90";
  22. // //public static string AGVSsqlname = "SH-CasunQRCodeDB";
  23. // //public static string GDIP = "192.168.1.100";
  24. // //public static int GDPort = 8899;
  25. // //public static string connectionString = "";
  26. // //public static string connectionString2 = "Data Source="+KWip+";Initial Catalog="+KWsqlname+" ;Integrated Security=false;uid=sa;pwd=123";
  27. // public DbHelperSQL()
  28. // {
  29. // }
  30. // #region 库位执行
  31. // public static DataSet Query(string SQLString, string connectionString)
  32. // {
  33. // using (SqlConnection connection = new SqlConnection(connectionString))
  34. // {
  35. // DataSet ds = new DataSet();
  36. // try
  37. // {
  38. // connection.Open();
  39. // SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
  40. // command.Fill(ds, "ds");
  41. // }
  42. // catch (System.Data.SqlClient.SqlException ex)
  43. // {
  44. // throw new Exception(ex.Message);
  45. // }
  46. // return ds;
  47. // }
  48. // }
  49. // public static int ExecuteSql2(string SQLString, string connectionString)
  50. // {
  51. // using (SqlConnection connection = new SqlConnection(connectionString))
  52. // {
  53. // using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  54. // {
  55. // try
  56. // {
  57. // connection.Open();
  58. // int rows = cmd.ExecuteNonQuery();
  59. // return rows;
  60. // }
  61. // catch (System.Data.SqlClient.SqlException e)
  62. // {
  63. // connection.Close();
  64. // throw e;
  65. // }
  66. // }
  67. // }
  68. // }
  69. // public static string EditDataSqlTran(List<string> strSqls)
  70. // {
  71. // if (strSqls?.Count <= 0)
  72. // {
  73. // // throw new Exception("请传入有效的SQL语句");
  74. // //MessageBox.Show("请选择要删除的项!");
  75. // return "请传入有效的SQL语句";
  76. // }
  77. // SqlConnection conn = null;
  78. // SqlTransaction sqltran = null;
  79. // try
  80. // {
  81. // conn = new SqlConnection(connectionString);
  82. // conn.Open();
  83. // sqltran = conn.BeginTransaction();
  84. // SqlCommand cmd = new SqlCommand();
  85. // cmd.Connection = conn;
  86. // cmd.Transaction = sqltran;
  87. // foreach (string str in strSqls)
  88. // {
  89. // if (string.IsNullOrWhiteSpace(str)) continue;
  90. // cmd.CommandText = str;
  91. // cmd.ExecuteNonQuery();
  92. // }
  93. // sqltran.Commit();
  94. // return "0";
  95. // }
  96. // catch (Exception ex)
  97. // {
  98. // sqltran.Rollback();
  99. // return ex.Message;
  100. // }
  101. // finally { conn.Close(); }
  102. // }
  103. // #endregion
  104. // #region 公用方法
  105. // /// <summary>
  106. // /// 判断是否存在某表的某个字段
  107. // /// </summary>
  108. // /// <param name="tableName">表名称</param>
  109. // /// <param name="columnName">列名称</param>
  110. // /// <returns>是否存在</returns>
  111. // public static bool ColumnExists(string tableName, string columnName)
  112. // {
  113. // string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'";
  114. // object res = GetSingle(sql);
  115. // if (res == null)
  116. // {
  117. // return false;
  118. // }
  119. // return Convert.ToInt32(res) > 0;
  120. // }
  121. // public static int GetMaxID(string FieldName, string TableName)
  122. // {
  123. // string strsql = "select max(" + FieldName + ")+1 from " + TableName;
  124. // object obj = GetSingle(strsql);
  125. // if (obj == null)
  126. // {
  127. // return 1;
  128. // }
  129. // else
  130. // {
  131. // return int.Parse(obj.ToString());
  132. // }
  133. // }
  134. // public static bool Exists(string strSql)
  135. // {
  136. // object obj = GetSingle(strSql);
  137. // int cmdresult;
  138. // if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  139. // {
  140. // cmdresult = 0;
  141. // }
  142. // else
  143. // {
  144. // cmdresult = int.Parse(obj.ToString());
  145. // }
  146. // if (cmdresult == 0)
  147. // {
  148. // return false;
  149. // }
  150. // else
  151. // {
  152. // return true;
  153. // }
  154. // }
  155. // /// <summary>
  156. // /// 表是否存在
  157. // /// </summary>
  158. // /// <param name="TableName"></param>
  159. // /// <returns></returns>
  160. // public static bool TabExists(string TableName)
  161. // {
  162. // string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
  163. // //string strsql = "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')";
  164. // object obj = GetSingle(strsql);
  165. // int cmdresult;
  166. // if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  167. // {
  168. // cmdresult = 0;
  169. // }
  170. // else
  171. // {
  172. // cmdresult = int.Parse(obj.ToString());
  173. // }
  174. // if (cmdresult == 0)
  175. // {
  176. // return false;
  177. // }
  178. // else
  179. // {
  180. // return true;
  181. // }
  182. // }
  183. // public static bool Exists(string strSql, params SqlParameter[] cmdParms)
  184. // {
  185. // object obj = GetSingle(strSql, cmdParms);
  186. // int cmdresult;
  187. // if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  188. // {
  189. // cmdresult = 0;
  190. // }
  191. // else
  192. // {
  193. // cmdresult = int.Parse(obj.ToString());
  194. // }
  195. // if (cmdresult == 0)
  196. // {
  197. // return false;
  198. // }
  199. // else
  200. // {
  201. // return true;
  202. // }
  203. // }
  204. // #endregion
  205. // #region 执行简单SQL语句
  206. // /// <summary>
  207. // /// 执行SQL语句,返回影响的记录数
  208. // /// </summary>
  209. // /// <param name="SQLString">SQL语句</param>
  210. // /// <returns>影响的记录数</returns>
  211. // public static int ExecuteSql(string SQLString)
  212. // {
  213. // using (SqlConnection connection = new SqlConnection(connectionString))
  214. // {
  215. // using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  216. // {
  217. // try
  218. // {
  219. // connection.Open();
  220. // int rows = cmd.ExecuteNonQuery();
  221. // return rows;
  222. // }
  223. // catch (System.Data.SqlClient.SqlException e)
  224. // {
  225. // connection.Close();
  226. // throw e;
  227. // }
  228. // }
  229. // }
  230. // }
  231. // /// <summary>
  232. // /// 执行SQL语句,返回影响的记录数
  233. // /// </summary>
  234. // /// <param name="SQLString">SQL语句</param>
  235. // /// <returns>影响的记录数</returns>
  236. // public static string ExecuteScalarSql(string SQLString)
  237. // {
  238. // using (SqlConnection connection = new SqlConnection(connectionString))
  239. // {
  240. // using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  241. // {
  242. // try
  243. // {
  244. // connection.Open();
  245. // //int rows = cmd.ExecuteNonQuery();
  246. // string posState = cmd.ExecuteScalar().ToString();
  247. // return posState;
  248. // }
  249. // catch (System.Data.SqlClient.SqlException e)
  250. // {
  251. // connection.Close();
  252. // throw e;
  253. // }
  254. // }
  255. // }
  256. // }
  257. // public static int ExecuteSqlByTime(string SQLString, int Times)
  258. // {
  259. // using (SqlConnection connection = new SqlConnection(connectionString))
  260. // {
  261. // using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  262. // {
  263. // try
  264. // {
  265. // connection.Open();
  266. // cmd.CommandTimeout = Times;
  267. // int rows = cmd.ExecuteNonQuery();
  268. // return rows;
  269. // }
  270. // catch (System.Data.SqlClient.SqlException e)
  271. // {
  272. // connection.Close();
  273. // throw e;
  274. // }
  275. // }
  276. // }
  277. // }
  278. // /// <summary>
  279. // /// 执行Sql和Oracle滴混合事务
  280. // /// </summary>
  281. // /// <param name="list">SQL命令行列表</param>
  282. // /// <param name="oracleCmdSqlList">Oracle命令行列表</param>
  283. // /// <returns>执行结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执行成功</returns>
  284. // //public static int ExecuteSqlTran(List<CommandInfo> list, List<CommandInfo> oracleCmdSqlList)
  285. // //{
  286. // // using (SqlConnection conn = new SqlConnection(connectionString))
  287. // // {
  288. // // conn.Open();
  289. // // SqlCommand cmd = new SqlCommand();
  290. // // cmd.Connection = conn;
  291. // // SqlTransaction tx = conn.BeginTransaction();
  292. // // cmd.Transaction = tx;
  293. // // try
  294. // // {
  295. // // foreach (CommandInfo myDE in list)
  296. // // {
  297. // // string cmdText = myDE.CommandText;
  298. // // SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
  299. // // PrepareCommand(cmd, conn, tx, cmdText, cmdParms);
  300. // // if (myDE.EffentNextType == EffentNextType.SolicitationEvent)
  301. // // {
  302. // // if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
  303. // // {
  304. // // tx.Rollback();
  305. // // throw new Exception("违背要求"+myDE.CommandText+"必须符合select count(..的格式");
  306. // // //return 0;
  307. // // }
  308. // // object obj = cmd.ExecuteScalar();
  309. // // bool isHave = false;
  310. // // if (obj == null && obj == DBNull.Value)
  311. // // {
  312. // // isHave = false;
  313. // // }
  314. // // isHave = Convert.ToInt32(obj) > 0;
  315. // // if (isHave)
  316. // // {
  317. // // //引发事件
  318. // // myDE.OnSolicitationEvent();
  319. // // }
  320. // // }
  321. // // if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
  322. // // {
  323. // // if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
  324. // // {
  325. // // tx.Rollback();
  326. // // throw new Exception("SQL:违背要求" + myDE.CommandText + "必须符合select count(..的格式");
  327. // // //return 0;
  328. // // }
  329. // // object obj = cmd.ExecuteScalar();
  330. // // bool isHave = false;
  331. // // if (obj == null && obj == DBNull.Value)
  332. // // {
  333. // // isHave = false;
  334. // // }
  335. // // isHave = Convert.ToInt32(obj) > 0;
  336. // // if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
  337. // // {
  338. // // tx.Rollback();
  339. // // throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须大于0");
  340. // // //return 0;
  341. // // }
  342. // // if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
  343. // // {
  344. // // tx.Rollback();
  345. // // throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须等于0");
  346. // // //return 0;
  347. // // }
  348. // // continue;
  349. // // }
  350. // // int val = cmd.ExecuteNonQuery();
  351. // // if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
  352. // // {
  353. // // tx.Rollback();
  354. // // throw new Exception("SQL:违背要求" + myDE.CommandText + "必须有影响行");
  355. // // //return 0;
  356. // // }
  357. // // cmd.Parameters.Clear();
  358. // // }
  359. // // string oraConnectionString = PubConstant.GetConnectionString("ConnectionStringPPC");
  360. // // bool res = OracleHelper.ExecuteSqlTran(oraConnectionString, oracleCmdSqlList);
  361. // // if (!res)
  362. // // {
  363. // // tx.Rollback();
  364. // // throw new Exception("Oracle执行失败");
  365. // // // return -1;
  366. // // }
  367. // // tx.Commit();
  368. // // return 1;
  369. // // }
  370. // // catch (System.Data.SqlClient.SqlException e)
  371. // // {
  372. // // tx.Rollback();
  373. // // throw e;
  374. // // }
  375. // // catch (Exception e)
  376. // // {
  377. // // tx.Rollback();
  378. // // throw e;
  379. // // }
  380. // // }
  381. // //}
  382. // /// <summary>
  383. // /// 执行多条SQL语句,实现数据库事务。
  384. // /// </summary>
  385. // /// <param name="SQLStringList">多条SQL语句</param>
  386. // public static int ExecuteSqlTran(List<String> SQLStringList)
  387. // {
  388. // using (SqlConnection conn = new SqlConnection(connectionString))
  389. // {
  390. // conn.Open();
  391. // SqlCommand cmd = new SqlCommand();
  392. // cmd.Connection = conn;
  393. // SqlTransaction tx = conn.BeginTransaction();
  394. // cmd.Transaction = tx;
  395. // try
  396. // {
  397. // int count = 0;
  398. // for (int n = 0; n < SQLStringList.Count; n++)
  399. // {
  400. // string strsql = SQLStringList[n];
  401. // if (strsql.Trim().Length > 1)
  402. // {
  403. // cmd.CommandText = strsql;
  404. // count += cmd.ExecuteNonQuery();
  405. // }
  406. // }
  407. // tx.Commit();
  408. // return count;
  409. // }
  410. // catch
  411. // {
  412. // tx.Rollback();
  413. // return 0;
  414. // }
  415. // }
  416. // }
  417. // /// <summary>
  418. // /// 执行带一个存储过程参数的的SQL语句。
  419. // /// </summary>
  420. // /// <param name="SQLString">SQL语句</param>
  421. // /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  422. // /// <returns>影响的记录数</returns>
  423. // public static int ExecuteSql(string SQLString, string content)
  424. // {
  425. // using (SqlConnection connection = new SqlConnection(connectionString))
  426. // {
  427. // SqlCommand cmd = new SqlCommand(SQLString, connection);
  428. // System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
  429. // myParameter.Value = content;
  430. // cmd.Parameters.Add(myParameter);
  431. // try
  432. // {
  433. // connection.Open();
  434. // int rows = cmd.ExecuteNonQuery();
  435. // return rows;
  436. // }
  437. // catch (System.Data.SqlClient.SqlException e)
  438. // {
  439. // throw e;
  440. // }
  441. // finally
  442. // {
  443. // cmd.Dispose();
  444. // connection.Close();
  445. // }
  446. // }
  447. // }
  448. // /// <summary>
  449. // /// 执行带一个存储过程参数的的SQL语句。
  450. // /// </summary>
  451. // /// <param name="SQLString">SQL语句</param>
  452. // /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  453. // /// <returns>影响的记录数</returns>
  454. // public static object ExecuteSqlGet(string SQLString, string content)
  455. // {
  456. // using (SqlConnection connection = new SqlConnection(connectionString))
  457. // {
  458. // SqlCommand cmd = new SqlCommand(SQLString, connection);
  459. // System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
  460. // myParameter.Value = content;
  461. // cmd.Parameters.Add(myParameter);
  462. // try
  463. // {
  464. // connection.Open();
  465. // object obj = cmd.ExecuteScalar();
  466. // if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  467. // {
  468. // return null;
  469. // }
  470. // else
  471. // {
  472. // return obj;
  473. // }
  474. // }
  475. // catch (System.Data.SqlClient.SqlException e)
  476. // {
  477. // throw e;
  478. // }
  479. // finally
  480. // {
  481. // cmd.Dispose();
  482. // connection.Close();
  483. // }
  484. // }
  485. // }
  486. // /// <summary>
  487. // /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
  488. // /// </summary>
  489. // /// <param name="strSQL">SQL语句</param>
  490. // /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
  491. // /// <returns>影响的记录数</returns>
  492. // public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
  493. // {
  494. // using (SqlConnection connection = new SqlConnection(connectionString))
  495. // {
  496. // SqlCommand cmd = new SqlCommand(strSQL, connection);
  497. // System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);
  498. // myParameter.Value = fs;
  499. // cmd.Parameters.Add(myParameter);
  500. // try
  501. // {
  502. // connection.Open();
  503. // int rows = cmd.ExecuteNonQuery();
  504. // return rows;
  505. // }
  506. // catch (System.Data.SqlClient.SqlException e)
  507. // {
  508. // throw e;
  509. // }
  510. // finally
  511. // {
  512. // cmd.Dispose();
  513. // connection.Close();
  514. // }
  515. // }
  516. // }
  517. // /// <summary>
  518. // /// 执行一条计算查询结果语句,返回查询结果(object)。
  519. // /// </summary>
  520. // /// <param name="SQLString">计算查询结果语句</param>
  521. // /// <returns>查询结果(object)</returns>
  522. // public static object GetSingle(string SQLString)
  523. // {
  524. // using (SqlConnection connection = new SqlConnection(connectionString))
  525. // {
  526. // using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  527. // {
  528. // try
  529. // {
  530. // connection.Open();
  531. // object obj = cmd.ExecuteScalar();
  532. // if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  533. // {
  534. // return null;
  535. // }
  536. // else
  537. // {
  538. // return obj;
  539. // }
  540. // }
  541. // catch (System.Data.SqlClient.SqlException e)
  542. // {
  543. // connection.Close();
  544. // throw e;
  545. // }
  546. // }
  547. // }
  548. // }
  549. // public static object GetSingle(string SQLString, int Times)
  550. // {
  551. // using (SqlConnection connection = new SqlConnection(connectionString))
  552. // {
  553. // using (SqlCommand cmd = new SqlCommand(SQLString, connection))
  554. // {
  555. // try
  556. // {
  557. // connection.Open();
  558. // cmd.CommandTimeout = Times;
  559. // object obj = cmd.ExecuteScalar();
  560. // if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  561. // {
  562. // return null;
  563. // }
  564. // else
  565. // {
  566. // return obj;
  567. // }
  568. // }
  569. // catch (System.Data.SqlClient.SqlException e)
  570. // {
  571. // connection.Close();
  572. // throw e;
  573. // }
  574. // }
  575. // }
  576. // }
  577. // /// <summary>
  578. // /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
  579. // /// </summary>
  580. // /// <param name="strSQL">查询语句</param>
  581. // /// <returns>SqlDataReader</returns>
  582. // public static SqlDataReader ExecuteReader(string strSQL)
  583. // {
  584. // SqlConnection connection = new SqlConnection(connectionString);
  585. // SqlCommand cmd = new SqlCommand(strSQL, connection);
  586. // try
  587. // {
  588. // connection.Open();
  589. // SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  590. // return myReader;
  591. // }
  592. // catch (System.Data.SqlClient.SqlException e)
  593. // {
  594. // throw e;
  595. // }
  596. // }
  597. // /// <summary>
  598. // /// 执行查询语句,返回DataSet
  599. // /// </summary>
  600. // /// <param name="SQLString">查询语句</param>
  601. // /// <returns>DataSet</returns>
  602. // public static DataSet Query(string SQLString)
  603. // {
  604. // using (SqlConnection connection = new SqlConnection(connectionString))
  605. // {
  606. // DataSet ds = new DataSet();
  607. // try
  608. // {
  609. // connection.Open();
  610. // SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
  611. // command.Fill(ds, "ds");
  612. // }
  613. // catch (System.Data.SqlClient.SqlException ex)
  614. // {
  615. // throw new Exception(ex.Message);
  616. // }
  617. // return ds;
  618. // }
  619. // }
  620. // public static DataSet Query(string SQLString, int Times)
  621. // {
  622. // using (SqlConnection connection = new SqlConnection(connectionString))
  623. // {
  624. // DataSet ds = new DataSet();
  625. // try
  626. // {
  627. // connection.Open();
  628. // SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
  629. // command.SelectCommand.CommandTimeout = Times;
  630. // command.Fill(ds, "ds");
  631. // }
  632. // catch (System.Data.SqlClient.SqlException ex)
  633. // {
  634. // throw new Exception(ex.Message);
  635. // }
  636. // return ds;
  637. // }
  638. // }
  639. // #endregion
  640. // #region 执行带参数的SQL语句
  641. // /// <summary>
  642. // /// 执行SQL语句,返回影响的记录数
  643. // /// </summary>
  644. // /// <param name="SQLString">SQL语句</param>
  645. // /// <returns>影响的记录数</returns>
  646. // public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
  647. // {
  648. // using (SqlConnection connection = new SqlConnection(connectionString))
  649. // {
  650. // using (SqlCommand cmd = new SqlCommand())
  651. // {
  652. // try
  653. // {
  654. // PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  655. // int rows = cmd.ExecuteNonQuery();
  656. // cmd.Parameters.Clear();
  657. // return rows;
  658. // }
  659. // catch (Exception ex)
  660. // {
  661. // return -1;
  662. // //throw e;
  663. // //System.
  664. // }
  665. // //catch (System.Data.SqlClient.SqlException e)
  666. // //{
  667. // // return -1;
  668. // // //throw e;
  669. // // //System.
  670. // //}
  671. // }
  672. // }
  673. // }
  674. // /// <summary>
  675. // /// 执行SQL语句,返回影响的记录数
  676. // /// </summary>
  677. // /// <param name="SQLString">SQL语句</param>
  678. // /// <returns>影响的记录数</returns>
  679. // public static string ExecuteScalarSql(string SQLString, params SqlParameter[] cmdParms)
  680. // {
  681. // using (SqlConnection connection = new SqlConnection(connectionString))
  682. // {
  683. // using (SqlCommand cmd = new SqlCommand())
  684. // {
  685. // try
  686. // {
  687. // PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  688. // string posState = cmd.ExecuteScalar().ToString();
  689. // cmd.Parameters.Clear();
  690. // return posState;
  691. // }
  692. // catch (System.Data.SqlClient.SqlException e)
  693. // {
  694. // connection.Close();
  695. // throw e;
  696. // }
  697. // }
  698. // }
  699. // }
  700. // /// <summary>
  701. // /// 执行多条SQL语句,实现数据库事务。
  702. // /// </summary>
  703. // /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
  704. // public static void ExecuteSqlTran(Hashtable SQLStringList)
  705. // {
  706. // using (SqlConnection conn = new SqlConnection(connectionString))
  707. // {
  708. // conn.Open();
  709. // using (SqlTransaction trans = conn.BeginTransaction())
  710. // {
  711. // SqlCommand cmd = new SqlCommand();
  712. // try
  713. // {
  714. // //循环
  715. // foreach (DictionaryEntry myDE in SQLStringList)
  716. // {
  717. // string cmdText = myDE.Key.ToString();
  718. // SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
  719. // PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  720. // int val = cmd.ExecuteNonQuery();
  721. // cmd.Parameters.Clear();
  722. // }
  723. // trans.Commit();
  724. // }
  725. // catch
  726. // {
  727. // trans.Rollback();
  728. // throw;
  729. // }
  730. // }
  731. // }
  732. // }
  733. // /// <summary>
  734. // /// 执行多条SQL语句,实现数据库事务。
  735. // /// </summary>
  736. // /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
  737. // //public static int ExecuteSqlTran(System.Collections.Generic.List<CommandInfo> cmdList)
  738. // //{
  739. // // using (SqlConnection conn = new SqlConnection(connectionString))
  740. // // {
  741. // // conn.Open();
  742. // // using (SqlTransaction trans = conn.BeginTransaction())
  743. // // {
  744. // // SqlCommand cmd = new SqlCommand();
  745. // // try
  746. // // {
  747. // // int count = 0;
  748. // // //循环
  749. // // foreach (CommandInfo myDE in cmdList)
  750. // // {
  751. // // string cmdText = myDE.CommandText;
  752. // // SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
  753. // // PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  754. // // if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
  755. // // {
  756. // // if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
  757. // // {
  758. // // trans.Rollback();
  759. // // return 0;
  760. // // }
  761. // // object obj = cmd.ExecuteScalar();
  762. // // bool isHave = false;
  763. // // if (obj == null && obj == DBNull.Value)
  764. // // {
  765. // // isHave = false;
  766. // // }
  767. // // isHave = Convert.ToInt32(obj) > 0;
  768. // // if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
  769. // // {
  770. // // trans.Rollback();
  771. // // return 0;
  772. // // }
  773. // // if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
  774. // // {
  775. // // trans.Rollback();
  776. // // return 0;
  777. // // }
  778. // // continue;
  779. // // }
  780. // // int val = cmd.ExecuteNonQuery();
  781. // // count += val;
  782. // // if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
  783. // // {
  784. // // trans.Rollback();
  785. // // return 0;
  786. // // }
  787. // // cmd.Parameters.Clear();
  788. // // }
  789. // // trans.Commit();
  790. // // return count;
  791. // // }
  792. // // catch
  793. // // {
  794. // // trans.Rollback();
  795. // // throw;
  796. // // }
  797. // // }
  798. // // }
  799. // //}
  800. // /// <summary>
  801. // /// 执行多条SQL语句,实现数据库事务。
  802. // /// </summary>
  803. // /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
  804. // //public static void ExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo> SQLStringList)
  805. // //{
  806. // // using (SqlConnection conn = new SqlConnection(connectionString))
  807. // // {
  808. // // conn.Open();
  809. // // using (SqlTransaction trans = conn.BeginTransaction())
  810. // // {
  811. // // SqlCommand cmd = new SqlCommand();
  812. // // try
  813. // // {
  814. // // int indentity = 0;
  815. // // //循环
  816. // // foreach (CommandInfo myDE in SQLStringList)
  817. // // {
  818. // // string cmdText = myDE.CommandText;
  819. // // SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters;
  820. // // foreach (SqlParameter q in cmdParms)
  821. // // {
  822. // // if (q.Direction == ParameterDirection.InputOutput)
  823. // // {
  824. // // q.Value = indentity;
  825. // // }
  826. // // }
  827. // // PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  828. // // int val = cmd.ExecuteNonQuery();
  829. // // foreach (SqlParameter q in cmdParms)
  830. // // {
  831. // // if (q.Direction == ParameterDirection.Output)
  832. // // {
  833. // // indentity = Convert.ToInt32(q.Value);
  834. // // }
  835. // // }
  836. // // cmd.Parameters.Clear();
  837. // // }
  838. // // trans.Commit();
  839. // // }
  840. // // catch
  841. // // {
  842. // // trans.Rollback();
  843. // // throw;
  844. // // }
  845. // // }
  846. // // }
  847. // //}
  848. // /// <summary>
  849. // /// 执行多条SQL语句,实现数据库事务。
  850. // /// </summary>
  851. // /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
  852. // public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
  853. // {
  854. // using (SqlConnection conn = new SqlConnection(connectionString))
  855. // {
  856. // conn.Open();
  857. // using (SqlTransaction trans = conn.BeginTransaction())
  858. // {
  859. // SqlCommand cmd = new SqlCommand();
  860. // try
  861. // {
  862. // int indentity = 0;
  863. // //循环
  864. // foreach (DictionaryEntry myDE in SQLStringList)
  865. // {
  866. // string cmdText = myDE.Key.ToString();
  867. // SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
  868. // foreach (SqlParameter q in cmdParms)
  869. // {
  870. // if (q.Direction == ParameterDirection.InputOutput)
  871. // {
  872. // q.Value = indentity;
  873. // }
  874. // }
  875. // PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  876. // int val = cmd.ExecuteNonQuery();
  877. // foreach (SqlParameter q in cmdParms)
  878. // {
  879. // if (q.Direction == ParameterDirection.Output)
  880. // {
  881. // indentity = Convert.ToInt32(q.Value);
  882. // }
  883. // }
  884. // cmd.Parameters.Clear();
  885. // }
  886. // trans.Commit();
  887. // }
  888. // catch
  889. // {
  890. // trans.Rollback();
  891. // throw;
  892. // }
  893. // }
  894. // }
  895. // }
  896. // /// <summary>
  897. // /// 执行一条计算查询结果语句,返回查询结果(object)。
  898. // /// </summary>
  899. // /// <param name="SQLString">计算查询结果语句</param>
  900. // /// <returns>查询结果(object)</returns>
  901. // public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)
  902. // {
  903. // using (SqlConnection connection = new SqlConnection(connectionString))
  904. // {
  905. // using (SqlCommand cmd = new SqlCommand())
  906. // {
  907. // try
  908. // {
  909. // PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  910. // object obj = cmd.ExecuteScalar();
  911. // cmd.Parameters.Clear();
  912. // if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  913. // {
  914. // return null;
  915. // }
  916. // else
  917. // {
  918. // return obj;
  919. // }
  920. // }
  921. // catch (System.Data.SqlClient.SqlException e)
  922. // {
  923. // //throw e;
  924. // return null;
  925. // }
  926. // }
  927. // }
  928. // }
  929. // /// <summary>
  930. // /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
  931. // /// </summary>
  932. // /// <param name="strSQL">查询语句</param>
  933. // /// <returns>SqlDataReader</returns>
  934. // public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms)
  935. // {
  936. // SqlConnection connection = new SqlConnection(connectionString);
  937. // SqlCommand cmd = new SqlCommand();
  938. // try
  939. // {
  940. // PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  941. // SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  942. // cmd.Parameters.Clear();
  943. // return myReader;
  944. // }
  945. // catch (System.Data.SqlClient.SqlException e)
  946. // {
  947. // throw e;
  948. // }
  949. // }
  950. // /// <summary>
  951. // /// 执行查询语句,返回DataSet
  952. // /// </summary>
  953. // /// <param name="SQLString">查询语句</param>
  954. // /// <returns>DataSet</returns>
  955. // public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)
  956. // {
  957. // using (SqlConnection connection = new SqlConnection(connectionString))
  958. // {
  959. // SqlCommand cmd = new SqlCommand();
  960. // PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  961. // using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  962. // {
  963. // DataSet ds = new DataSet();
  964. // try
  965. // {
  966. // da.Fill(ds, "ds");
  967. // cmd.Parameters.Clear();
  968. // }
  969. // catch (System.Data.SqlClient.SqlException ex)
  970. // {
  971. // throw new Exception(ex.Message);
  972. // }
  973. // return ds;
  974. // }
  975. // }
  976. // }
  977. // private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
  978. // {
  979. // if (conn.State != ConnectionState.Open)
  980. // conn.Open();
  981. // cmd.Connection = conn;
  982. // cmd.CommandText = cmdText;
  983. // if (trans != null)
  984. // cmd.Transaction = trans;
  985. // cmd.CommandType = CommandType.Text;//cmdType;
  986. // if (cmdParms != null)
  987. // {
  988. // foreach (SqlParameter parameter in cmdParms)
  989. // {
  990. // if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  991. // (parameter.Value == null))
  992. // {
  993. // parameter.Value = DBNull.Value;
  994. // }
  995. // cmd.Parameters.Add(parameter);
  996. // }
  997. // }
  998. // }
  999. // #endregion
  1000. // #region 存储过程操作
  1001. // /// <summary>
  1002. // /// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
  1003. // /// </summary>
  1004. // /// <param name="storedProcName">存储过程名</param>
  1005. // /// <param name="parameters">存储过程参数</param>
  1006. // /// <returns>SqlDataReader</returns>
  1007. // public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
  1008. // {
  1009. // SqlConnection connection = new SqlConnection(connectionString);
  1010. // SqlDataReader returnReader;
  1011. // connection.Open();
  1012. // SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
  1013. // command.CommandType = CommandType.StoredProcedure;
  1014. // returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
  1015. // return returnReader;
  1016. // }
  1017. // /// <summary>
  1018. // /// 执行存储过程
  1019. // /// </summary>
  1020. // /// <param name="storedProcName">存储过程名</param>
  1021. // /// <param name="parameters">存储过程参数</param>
  1022. // /// <param name="tableName">DataSet结果中的表名</param>
  1023. // /// <returns>DataSet</returns>
  1024. // public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
  1025. // {
  1026. // using (SqlConnection connection = new SqlConnection(connectionString))
  1027. // {
  1028. // DataSet dataSet = new DataSet();
  1029. // connection.Open();
  1030. // SqlDataAdapter sqlDA = new SqlDataAdapter();
  1031. // sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
  1032. // sqlDA.Fill(dataSet, tableName);
  1033. // connection.Close();
  1034. // return dataSet;
  1035. // }
  1036. // }
  1037. // public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
  1038. // {
  1039. // using (SqlConnection connection = new SqlConnection(connectionString))
  1040. // {
  1041. // DataSet dataSet = new DataSet();
  1042. // connection.Open();
  1043. // SqlDataAdapter sqlDA = new SqlDataAdapter();
  1044. // sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
  1045. // sqlDA.SelectCommand.CommandTimeout = Times;
  1046. // sqlDA.Fill(dataSet, tableName);
  1047. // connection.Close();
  1048. // return dataSet;
  1049. // }
  1050. // }
  1051. // /// <summary>
  1052. // /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
  1053. // /// </summary>
  1054. // /// <param name="connection">数据库连接</param>
  1055. // /// <param name="storedProcName">存储过程名</param>
  1056. // /// <param name="parameters">存储过程参数</param>
  1057. // /// <returns>SqlCommand</returns>
  1058. // private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
  1059. // {
  1060. // SqlCommand command = new SqlCommand(storedProcName, connection);
  1061. // command.CommandType = CommandType.StoredProcedure;
  1062. // foreach (SqlParameter parameter in parameters)
  1063. // {
  1064. // if (parameter != null)
  1065. // {
  1066. // // 检查未分配值的输出参数,将其分配以DBNull.Value.
  1067. // if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  1068. // (parameter.Value == null))
  1069. // {
  1070. // parameter.Value = DBNull.Value;
  1071. // }
  1072. // command.Parameters.Add(parameter);
  1073. // }
  1074. // }
  1075. // return command;
  1076. // }
  1077. // /// <summary>
  1078. // /// 执行存储过程,返回影响的行数
  1079. // /// </summary>
  1080. // /// <param name="storedProcName">存储过程名</param>
  1081. // /// <param name="parameters">存储过程参数</param>
  1082. // /// <param name="rowsAffected">影响的行数</param>
  1083. // /// <returns></returns>
  1084. // public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
  1085. // {
  1086. // using (SqlConnection connection = new SqlConnection(connectionString))
  1087. // {
  1088. // int result;
  1089. // connection.Open();
  1090. // SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
  1091. // rowsAffected = command.ExecuteNonQuery();
  1092. // result = (int)command.Parameters["ReturnValue"].Value;
  1093. // //Connection.Close();
  1094. // return result;
  1095. // }
  1096. // }
  1097. // /// <summary>
  1098. // /// 创建 SqlCommand 对象实例(用来返回一个整数值)
  1099. // /// </summary>
  1100. // /// <param name="storedProcName">存储过程名</param>
  1101. // /// <param name="parameters">存储过程参数</param>
  1102. // /// <returns>SqlCommand 对象实例</returns>
  1103. // private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
  1104. // {
  1105. // SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
  1106. // command.Parameters.Add(new SqlParameter("ReturnValue",
  1107. // SqlDbType.Int, 4, ParameterDirection.ReturnValue,
  1108. // false, 0, 0, string.Empty, DataRowVersion.Default, null));
  1109. // return command;
  1110. // }
  1111. // #endregion
  1112. //}
  1113. }