地铁二期项目正式开始

DbHelperMYSQL.cs 55KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using System.Data;
  5. using MySql.Data.MySqlClient;
  6. using System.Configuration;
  7. using System.Data.Common;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using YTSoft.DBUtility;
  11. namespace DBUtility
  12. {
  13. /// <summary>
  14. /// 数据访问抽象基础类
  15. /// Copyright (C) Maticsoft
  16. /// </summary>
  17. public abstract class DbHelperMYSQL
  18. {
  19. //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
  20. public static readonly string connectionString = ConfigurationManager.ConnectionStrings["MySqlconnection"].ConnectionString;
  21. public DbHelperMYSQL()
  22. {
  23. }
  24. /// <summary>
  25. /// DataTable to tree 根据数据id,txt生成对应的json数据格式
  26. /// </summary>
  27. /// <param name="dt"></param>
  28. /// <param name="pField">Parentid</param>
  29. /// <param name="pValue">Parentid value</param>
  30. /// <param name="kField">pkid </param>
  31. /// <returns></returns>
  32. public static string TableToEasyUITreeJson(DataTable dt, string pField, string pValue, string pkid, string pktext, string url)
  33. {
  34. try
  35. {
  36. StringBuilder sb = new StringBuilder();
  37. string filter = String.Format(" {0}='{1}' ", pField, pValue);//获取顶级目录
  38. if (pValue == string.Empty || pValue == null)
  39. {
  40. filter = String.Format("{0}='{1}' or {0} is null", pField, pValue);
  41. }
  42. //filter = "Parentid is null";
  43. DataRow[] drs = dt.Select(filter);
  44. if (drs.Length < 1) return "";
  45. sb.Append("<UL id='treeList'>\n");
  46. foreach (DataRow dr in drs)
  47. {
  48. string pcv = dr[pkid].ToString();
  49. sb.AppendFormat("<li id='tree{0}'><a href='list.aspx?typeId={0}&ptypeId=#$' title='{1}'>\n", dr[pkid].ToString(), dr[pktext].ToString());
  50. sb.AppendFormat("{0}\n</a>", dr[pktext].ToString());
  51. //sb.Append("\"checked\":true,");
  52. //if (pValue == null || pValue == string.Empty)//根节点没有url
  53. //{
  54. // sb.AppendFormat("\"attributes\":\"{0}\"", "");
  55. //}
  56. //else
  57. //{
  58. // sb.AppendFormat("\"attributes\":\"{0}\"", url);
  59. //}
  60. sb.Append(TableToEasyUITreeJson(dt, pField, pcv, pkid, pktext, url).TrimEnd(','));
  61. sb.Append("</li>\n");
  62. }
  63. //if (sb.ToString().EndsWith(","))
  64. //{
  65. // sb.Remove(sb.Length - 1, 1);
  66. //}
  67. sb.Append("</ul>\n");
  68. return sb.ToString();
  69. }
  70. catch (Exception)
  71. {
  72. return "";
  73. }
  74. }
  75. public static string TableToEasyUITreeJson(DataTable dt, string pField, string pValue, string pkid)
  76. {
  77. try
  78. {
  79. StringBuilder sb = new StringBuilder();
  80. string filter = String.Format(" {0}='{1}' ", pField, pValue);//获取顶级目录
  81. if (pValue == string.Empty || pValue == null)
  82. {
  83. filter = String.Format("{0}='{1}' or {0} is null", pField, pValue);
  84. }
  85. //filter = "Parentid is null";
  86. DataRow[] drs = dt.Select(filter);
  87. if (drs.Length < 1) return "";
  88. //sb.AppendFormat("{0},", pValue);
  89. foreach (DataRow dr in drs)
  90. {
  91. string pcv = dr[pkid].ToString();
  92. sb.AppendFormat("{0},", dr[pkid].ToString());
  93. sb.Append(TableToEasyUITreeJson(dt, pField, pcv, pkid));
  94. }
  95. return sb.ToString();
  96. }
  97. catch (Exception)
  98. {
  99. return "";
  100. }
  101. }
  102. public static DataTable getDataTable(string sql, params MySqlParameter[] pms)
  103. {
  104. DataTable dt;
  105. try
  106. {
  107. using (MySqlConnection connection = new MySqlConnection(connectionString))
  108. {
  109. using (MySqlCommand cmd = new MySqlCommand())
  110. {
  111. cmd.Connection = connection;
  112. cmd.CommandText = sql;
  113. if (connection.State != ConnectionState.Open)
  114. connection.Open();
  115. if (pms != null)
  116. {
  117. foreach (MySqlParameter parameter in pms)
  118. {
  119. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  120. (parameter.Value == null))
  121. {
  122. parameter.Value = DBNull.Value;
  123. }
  124. cmd.Parameters.Add(parameter);
  125. }
  126. }
  127. MySqlDataAdapter mda = new MySqlDataAdapter(cmd);
  128. dt = new DataTable();
  129. mda.Fill(dt);
  130. }
  131. }
  132. }
  133. catch (MySql.Data.MySqlClient.MySqlException e)
  134. {
  135. throw e;
  136. }
  137. return dt;
  138. }
  139. #region 公用方法
  140. /// <summary>
  141. /// 得到最大值
  142. /// </summary>
  143. /// <param name="FieldName"></param>
  144. /// <param name="TableName"></param>
  145. /// <returns></returns>
  146. public static int GetMaxID(string FieldName, string TableName)
  147. {
  148. string strsql = "select max(" + FieldName + ")+1 from " + TableName;
  149. object obj = GetSingle(strsql);
  150. if (obj == null)
  151. {
  152. return 1;
  153. }
  154. else
  155. {
  156. return int.Parse(obj.ToString());
  157. }
  158. }
  159. /// <summary>
  160. /// 是否存在
  161. /// </summary>
  162. /// <param name="strSql"></param>
  163. /// <returns></returns>
  164. public static bool Exists(string strSql)
  165. {
  166. object obj = GetSingle(strSql);
  167. int cmdresult;
  168. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  169. {
  170. cmdresult = 0;
  171. }
  172. else
  173. {
  174. cmdresult = int.Parse(obj.ToString());
  175. }
  176. if (cmdresult == 0)
  177. {
  178. return false;
  179. }
  180. else
  181. {
  182. return true;
  183. }
  184. }
  185. /// <summary>
  186. /// 是否存在(基于MySqlParameter)
  187. /// </summary>
  188. /// <param name="strSql"></param>
  189. /// <param name="cmdParms"></param>
  190. /// <returns></returns>
  191. public static bool Exists(string strSql, params MySqlParameter[] cmdParms)
  192. {
  193. object obj = GetSingle(strSql, cmdParms);
  194. int cmdresult;
  195. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  196. {
  197. cmdresult = 0;
  198. }
  199. else
  200. {
  201. cmdresult = int.Parse(obj.ToString());
  202. }
  203. if (cmdresult == 0)
  204. {
  205. return false;
  206. }
  207. else
  208. {
  209. return true;
  210. }
  211. }
  212. #endregion
  213. #region 执行简单SQL语句
  214. /// <summary>
  215. /// 执行SQL语句,返回影响的记录数
  216. /// </summary>
  217. /// <param name="SQLString">SQL语句</param>
  218. /// <returns>影响的记录数</returns>
  219. public static int ExecuteSql(string SQLString)
  220. {
  221. using (MySqlConnection connection = new MySqlConnection(connectionString))
  222. {
  223. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  224. {
  225. try
  226. {
  227. connection.Open();
  228. int rows = cmd.ExecuteNonQuery();
  229. return rows;
  230. }
  231. catch (MySql.Data.MySqlClient.MySqlException e)
  232. {
  233. connection.Close();
  234. throw e;
  235. }
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// 执行SQL语句,返回影响的记录数
  241. /// </summary>
  242. /// <param name="SQLString">SQL语句</param>
  243. /// <returns>影响的记录数</returns>
  244. public static int ExecuteSqlConn(string SQLString, string connectStr)
  245. {
  246. using (MySqlConnection connection = new MySqlConnection(connectStr))
  247. {
  248. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  249. {
  250. try
  251. {
  252. connection.Open();
  253. int rows = cmd.ExecuteNonQuery();
  254. return rows;
  255. }
  256. catch (MySql.Data.MySqlClient.MySqlException e)
  257. {
  258. connection.Close();
  259. throw e;
  260. }
  261. }
  262. }
  263. }
  264. public static int ExecuteSqlByTime(string SQLString, int Times)
  265. {
  266. using (MySqlConnection connection = new MySqlConnection(connectionString))
  267. {
  268. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  269. {
  270. try
  271. {
  272. connection.Open();
  273. cmd.CommandTimeout = Times;
  274. int rows = cmd.ExecuteNonQuery();
  275. return rows;
  276. }
  277. catch (MySql.Data.MySqlClient.MySqlException e)
  278. {
  279. connection.Close();
  280. throw e;
  281. }
  282. }
  283. }
  284. }
  285. #region
  286. /// <summary>
  287. /// 执行MySql和Oracle滴混合事务
  288. /// </summary>
  289. /// <param name="list">SQL命令行列表</param>
  290. /// <param name="oracleCmdSqlList">Oracle命令行列表</param>
  291. /// <returns>执行结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执行成功</returns>
  292. //public static int ExecuteSqlTran(List<CommandInfo> list, List<CommandInfo> oracleCmdSqlList)
  293. //{
  294. // using (MySqlConnection conn = new MySqlConnection(connectionString))
  295. // {
  296. // conn.Open();
  297. // MySqlCommand cmd = new MySqlCommand();
  298. // cmd.Connection = conn;
  299. // MySqlTransaction tx = conn.BeginTransaction();
  300. // cmd.Transaction = tx;
  301. // try
  302. // {
  303. // foreach (CommandInfo myDE in list)
  304. // {
  305. // string cmdText = myDE.CommandText;
  306. // MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters;
  307. // PrepareCommand(cmd, conn, tx, cmdText, cmdParms);
  308. // if (myDE.EffentNextType == EffentNextType.SolicitationEvent)
  309. // {
  310. // if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
  311. // {
  312. // tx.Rollback();
  313. // throw new Exception("违背要求" + myDE.CommandText + "必须符合select count(..的格式");
  314. // //return 0;
  315. // }
  316. // object obj = cmd.ExecuteScalar();
  317. // bool isHave = false;
  318. // if (obj == null && obj == DBNull.Value)
  319. // {
  320. // isHave = false;
  321. // }
  322. // isHave = Convert.ToInt32(obj) > 0;
  323. // if (isHave)
  324. // {
  325. // //引发事件
  326. // myDE.OnSolicitationEvent();
  327. // }
  328. // }
  329. // if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
  330. // {
  331. // if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
  332. // {
  333. // tx.Rollback();
  334. // throw new Exception("SQL:违背要求" + myDE.CommandText + "必须符合select count(..的格式");
  335. // //return 0;
  336. // }
  337. // object obj = cmd.ExecuteScalar();
  338. // bool isHave = false;
  339. // if (obj == null && obj == DBNull.Value)
  340. // {
  341. // isHave = false;
  342. // }
  343. // isHave = Convert.ToInt32(obj) > 0;
  344. // if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
  345. // {
  346. // tx.Rollback();
  347. // throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须大于0");
  348. // //return 0;
  349. // }
  350. // if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
  351. // {
  352. // tx.Rollback();
  353. // throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须等于0");
  354. // //return 0;
  355. // }
  356. // continue;
  357. // }
  358. // int val = cmd.ExecuteNonQuery();
  359. // if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
  360. // {
  361. // tx.Rollback();
  362. // throw new Exception("SQL:违背要求" + myDE.CommandText + "必须有影响行");
  363. // //return 0;
  364. // }
  365. // cmd.Parameters.Clear();
  366. // }
  367. // string oraConnectionString = ConfigurationManager.ConnectionStrings["MySqlStr"].ConnectionString;
  368. // bool res = OracleHelper.ExecuteSqlTran(oraConnectionString, oracleCmdSqlList);
  369. // if (!res)
  370. // {
  371. // tx.Rollback();
  372. // throw new Exception("执行失败");
  373. // // return -1;
  374. // }
  375. // tx.Commit();
  376. // return 1;
  377. // }
  378. // catch (MySql.Data.MySqlClient.MySqlException e)
  379. // {
  380. // tx.Rollback();
  381. // throw e;
  382. // }
  383. // catch (Exception e)
  384. // {
  385. // tx.Rollback();
  386. // throw e;
  387. // }
  388. // }
  389. //}
  390. #endregion
  391. /// <summary>
  392. /// 执行多条SQL语句,实现数据库事务。
  393. /// </summary>
  394. /// <param name="SQLStringList">多条SQL语句</param>
  395. public static int ExecuteSqlTran(List<String> SQLStringList)
  396. {
  397. using (MySqlConnection conn = new MySqlConnection(connectionString))
  398. {
  399. conn.Open();
  400. MySqlCommand cmd = new MySqlCommand();
  401. cmd.Connection = conn;
  402. MySqlTransaction tx = conn.BeginTransaction();
  403. cmd.Transaction = tx;
  404. try
  405. {
  406. int count = 0;
  407. for (int n = 0; n < SQLStringList.Count; n++)
  408. {
  409. string strsql = SQLStringList[n];
  410. if (strsql.Trim().Length > 1)
  411. {
  412. cmd.CommandText = strsql;
  413. count += cmd.ExecuteNonQuery();
  414. }
  415. }
  416. tx.Commit();
  417. return count;
  418. }
  419. catch (Exception ex)
  420. {
  421. tx.Rollback();
  422. throw ex;
  423. return 0;
  424. }
  425. }
  426. }
  427. /// <summary>
  428. /// 执行带一个存储过程参数的的SQL语句。
  429. /// </summary>
  430. /// <param name="SQLString">SQL语句</param>
  431. /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  432. /// <returns>影响的记录数</returns>
  433. public static int ExecuteSql(string SQLString, string content)
  434. {
  435. using (MySqlConnection connection = new MySqlConnection(connectionString))
  436. {
  437. MySqlCommand cmd = new MySqlCommand(SQLString, connection);
  438. MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText);
  439. myParameter.Value = content;
  440. cmd.Parameters.Add(myParameter);
  441. try
  442. {
  443. connection.Open();
  444. int rows = cmd.ExecuteNonQuery();
  445. return rows;
  446. }
  447. catch (MySql.Data.MySqlClient.MySqlException e)
  448. {
  449. throw e;
  450. }
  451. finally
  452. {
  453. cmd.Dispose();
  454. connection.Close();
  455. }
  456. }
  457. }
  458. /// <summary>
  459. /// 执行带一个存储过程参数的的SQL语句。
  460. /// </summary>
  461. /// <param name="SQLString">SQL语句</param>
  462. /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  463. /// <returns>影响的记录数</returns>
  464. public static object ExecuteSqlGet(string SQLString, string content)
  465. {
  466. using (MySqlConnection connection = new MySqlConnection(connectionString))
  467. {
  468. MySqlCommand cmd = new MySqlCommand(SQLString, connection);
  469. MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText);
  470. myParameter.Value = content;
  471. cmd.Parameters.Add(myParameter);
  472. try
  473. {
  474. connection.Open();
  475. object obj = cmd.ExecuteScalar();
  476. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  477. {
  478. return null;
  479. }
  480. else
  481. {
  482. return obj;
  483. }
  484. }
  485. catch (MySql.Data.MySqlClient.MySqlException e)
  486. {
  487. throw e;
  488. }
  489. finally
  490. {
  491. cmd.Dispose();
  492. connection.Close();
  493. }
  494. }
  495. }
  496. /// <summary>
  497. /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
  498. /// </summary>
  499. /// <param name="strSQL">SQL语句</param>
  500. /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
  501. /// <returns>影响的记录数</returns>
  502. public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
  503. {
  504. using (MySqlConnection connection = new MySqlConnection(connectionString))
  505. {
  506. MySqlCommand cmd = new MySqlCommand(strSQL, connection);
  507. MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@fs", SqlDbType.Image);
  508. myParameter.Value = fs;
  509. cmd.Parameters.Add(myParameter);
  510. try
  511. {
  512. connection.Open();
  513. int rows = cmd.ExecuteNonQuery();
  514. return rows;
  515. }
  516. catch (MySql.Data.MySqlClient.MySqlException e)
  517. {
  518. throw e;
  519. }
  520. finally
  521. {
  522. cmd.Dispose();
  523. connection.Close();
  524. }
  525. }
  526. }
  527. /// <summary>
  528. /// 执行一条计算查询结果语句,返回查询结果(object)。
  529. /// </summary>
  530. /// <param name="SQLString">计算查询结果语句</param>
  531. /// <returns>查询结果(object)</returns>
  532. public static object GetSingle(string SQLString)
  533. {
  534. using (MySqlConnection connection = new MySqlConnection(connectionString))
  535. {
  536. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  537. {
  538. try
  539. {
  540. connection.Open();
  541. object obj = cmd.ExecuteScalar();
  542. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  543. {
  544. return null;
  545. }
  546. else
  547. {
  548. return obj;
  549. }
  550. }
  551. catch (MySql.Data.MySqlClient.MySqlException e)
  552. {
  553. connection.Close();
  554. throw e;
  555. }
  556. }
  557. }
  558. }
  559. public static object GetSingle(string SQLString, int Times)
  560. {
  561. using (MySqlConnection connection = new MySqlConnection(connectionString))
  562. {
  563. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  564. {
  565. try
  566. {
  567. connection.Open();
  568. cmd.CommandTimeout = Times;
  569. object obj = cmd.ExecuteScalar();
  570. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  571. {
  572. return null;
  573. }
  574. else
  575. {
  576. return obj;
  577. }
  578. }
  579. catch (MySql.Data.MySqlClient.MySqlException e)
  580. {
  581. connection.Close();
  582. throw e;
  583. }
  584. }
  585. }
  586. }
  587. /// <summary>
  588. /// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
  589. /// </summary>
  590. /// <param name="strSQL">查询语句</param>
  591. /// <returns>MySqlDataReader</returns>
  592. public static MySqlDataReader ExecuteReader(string strSQL)
  593. {
  594. MySqlConnection connection = new MySqlConnection(connectionString);
  595. MySqlCommand cmd = new MySqlCommand(strSQL, connection);
  596. try
  597. {
  598. connection.Open();
  599. MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  600. return myReader;
  601. }
  602. catch (MySql.Data.MySqlClient.MySqlException e)
  603. {
  604. throw e;
  605. }
  606. }
  607. /// <summary>
  608. /// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
  609. /// </summary>
  610. /// <param name="strSQL">查询语句</param>
  611. /// <returns>MySqlDataReader</returns>
  612. public static MySqlDataReader ExecuteReader(string strSQL, string connectionStr)
  613. {
  614. MySqlConnection connection = new MySqlConnection(connectionStr);
  615. MySqlCommand cmd = new MySqlCommand(strSQL, connection);
  616. try
  617. {
  618. connection.Open();
  619. MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  620. return myReader;
  621. }
  622. catch (MySql.Data.MySqlClient.MySqlException e)
  623. {
  624. throw e;
  625. }
  626. }
  627. /// <summary>
  628. /// 执行查询语句,返回DataSet
  629. /// </summary>
  630. /// <param name="SQLString">查询语句</param>
  631. /// <returns>DataSet</returns>
  632. public static DataSet Query(string SQLString)
  633. {
  634. using (MySqlConnection connection = new MySqlConnection(connectionString))
  635. {
  636. DataSet ds = new DataSet();
  637. try
  638. {
  639. connection.Open();
  640. MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
  641. command.Fill(ds, "ds");
  642. }
  643. catch (MySql.Data.MySqlClient.MySqlException ex)
  644. {
  645. throw new Exception(ex.Message);
  646. }
  647. return ds;
  648. }
  649. }
  650. /// <summary>
  651. /// 执行查询语句,返回DataSet
  652. /// </summary>
  653. /// <param name="SQLString">查询语句</param>
  654. /// <returns>DataSet</returns>
  655. public static DataSet QueryConn(string SQLString, string conStr)
  656. {
  657. using (MySqlConnection connection = new MySqlConnection(conStr))
  658. {
  659. DataSet ds = new DataSet();
  660. try
  661. {
  662. connection.Open();
  663. MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
  664. command.Fill(ds, "ds");
  665. }
  666. catch (MySql.Data.MySqlClient.MySqlException ex)
  667. {
  668. throw new Exception(ex.Message);
  669. }
  670. finally
  671. {
  672. connection.Close();
  673. }
  674. return ds;
  675. }
  676. }
  677. public static DataSet Query(string SQLString, int Times)
  678. {
  679. using (MySqlConnection connection = new MySqlConnection(connectionString))
  680. {
  681. DataSet ds = new DataSet();
  682. try
  683. {
  684. connection.Open();
  685. MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
  686. command.SelectCommand.CommandTimeout = Times;
  687. command.Fill(ds, "ds");
  688. }
  689. catch (MySql.Data.MySqlClient.MySqlException ex)
  690. {
  691. throw new Exception(ex.Message);
  692. }
  693. return ds;
  694. }
  695. }
  696. #endregion
  697. #region 执行带参数的SQL语句
  698. /// <summary>
  699. /// 执行SQL语句,返回影响的记录数
  700. /// </summary>
  701. /// <param name="SQLString">SQL语句</param>
  702. /// <returns>影响的记录数</returns>
  703. public static int ExecuteSql(string SQLString, params MySqlParameter[] cmdParms)
  704. {
  705. using (MySqlConnection connection = new MySqlConnection(connectionString))
  706. {
  707. using (MySqlCommand cmd = new MySqlCommand())
  708. {
  709. try
  710. {
  711. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  712. int rows = cmd.ExecuteNonQuery();
  713. cmd.Parameters.Clear();
  714. return rows;
  715. }
  716. catch (MySql.Data.MySqlClient.MySqlException e)
  717. {
  718. throw e;
  719. }
  720. }
  721. }
  722. }
  723. /// <summary>
  724. /// 执行多条SQL语句,实现数据库事务。
  725. /// </summary>
  726. /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param>
  727. public static void ExecuteSqlTran(Hashtable SQLStringList)
  728. {
  729. using (MySqlConnection conn = new MySqlConnection(connectionString))
  730. {
  731. conn.Open();
  732. using (MySqlTransaction trans = conn.BeginTransaction())
  733. {
  734. MySqlCommand cmd = new MySqlCommand();
  735. try
  736. {
  737. //循环
  738. foreach (DictionaryEntry myDE in SQLStringList)
  739. {
  740. string cmdText = myDE.Key.ToString();
  741. MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Value;
  742. PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  743. int val = cmd.ExecuteNonQuery();
  744. cmd.Parameters.Clear();
  745. }
  746. trans.Commit();
  747. }
  748. catch
  749. {
  750. trans.Rollback();
  751. throw;
  752. }
  753. }
  754. }
  755. }
  756. /// <summary>
  757. /// 执行多条SQL语句,实现数据库事务。
  758. /// </summary>
  759. /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param>
  760. public static int ExecuteSqlTran(System.Collections.Generic.List<CommandInfo> cmdList)
  761. {
  762. using (MySqlConnection conn = new MySqlConnection(connectionString))
  763. {
  764. conn.Open();
  765. using (MySqlTransaction trans = conn.BeginTransaction())
  766. {
  767. MySqlCommand cmd = new MySqlCommand();
  768. try
  769. {
  770. int count = 0;
  771. //循环
  772. foreach (CommandInfo myDE in cmdList)
  773. {
  774. string cmdText = myDE.CommandText;
  775. MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters;
  776. PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  777. if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
  778. {
  779. if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
  780. {
  781. trans.Rollback();
  782. return 0;
  783. }
  784. object obj = cmd.ExecuteScalar();
  785. bool isHave = false;
  786. if (obj == null && obj == DBNull.Value)
  787. {
  788. isHave = false;
  789. }
  790. isHave = Convert.ToInt32(obj) > 0;
  791. if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
  792. {
  793. trans.Rollback();
  794. return 0;
  795. }
  796. if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
  797. {
  798. trans.Rollback();
  799. return 0;
  800. }
  801. continue;
  802. }
  803. int val = cmd.ExecuteNonQuery();
  804. count += val;
  805. if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
  806. {
  807. trans.Rollback();
  808. return 0;
  809. }
  810. cmd.Parameters.Clear();
  811. }
  812. trans.Commit();
  813. return count;
  814. }
  815. catch
  816. {
  817. trans.Rollback();
  818. throw;
  819. }
  820. }
  821. }
  822. }
  823. /// <summary>
  824. /// 执行多条SQL语句,实现数据库事务。
  825. /// </summary>
  826. /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param>
  827. public static void ExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo> SQLStringList)
  828. {
  829. using (MySqlConnection conn = new MySqlConnection(connectionString))
  830. {
  831. conn.Open();
  832. using (MySqlTransaction trans = conn.BeginTransaction())
  833. {
  834. MySqlCommand cmd = new MySqlCommand();
  835. try
  836. {
  837. int indentity = 0;
  838. //循环
  839. foreach (CommandInfo myDE in SQLStringList)
  840. {
  841. string cmdText = myDE.CommandText;
  842. MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters;
  843. foreach (MySqlParameter q in cmdParms)
  844. {
  845. if (q.Direction == ParameterDirection.InputOutput)
  846. {
  847. q.Value = indentity;
  848. }
  849. }
  850. PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  851. int val = cmd.ExecuteNonQuery();
  852. foreach (MySqlParameter q in cmdParms)
  853. {
  854. if (q.Direction == ParameterDirection.Output)
  855. {
  856. indentity = Convert.ToInt32(q.Value);
  857. }
  858. }
  859. cmd.Parameters.Clear();
  860. }
  861. trans.Commit();
  862. }
  863. catch
  864. {
  865. trans.Rollback();
  866. throw;
  867. }
  868. }
  869. }
  870. }
  871. /// <summary>
  872. /// 执行多条SQL语句,实现数据库事务。
  873. /// </summary>
  874. /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param>
  875. public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
  876. {
  877. using (MySqlConnection conn = new MySqlConnection(connectionString))
  878. {
  879. conn.Open();
  880. using (MySqlTransaction trans = conn.BeginTransaction())
  881. {
  882. MySqlCommand cmd = new MySqlCommand();
  883. try
  884. {
  885. int indentity = 0;
  886. //循环
  887. foreach (DictionaryEntry myDE in SQLStringList)
  888. {
  889. string cmdText = myDE.Key.ToString();
  890. MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Value;
  891. foreach (MySqlParameter q in cmdParms)
  892. {
  893. if (q.Direction == ParameterDirection.InputOutput)
  894. {
  895. q.Value = indentity;
  896. }
  897. }
  898. PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  899. int val = cmd.ExecuteNonQuery();
  900. foreach (MySqlParameter q in cmdParms)
  901. {
  902. if (q.Direction == ParameterDirection.Output)
  903. {
  904. indentity = Convert.ToInt32(q.Value);
  905. }
  906. }
  907. cmd.Parameters.Clear();
  908. }
  909. trans.Commit();
  910. }
  911. catch
  912. {
  913. trans.Rollback();
  914. throw;
  915. }
  916. }
  917. }
  918. }
  919. /// <summary>
  920. /// 执行一条计算查询结果语句,返回查询结果(object)。
  921. /// </summary>
  922. /// <param name="SQLString">计算查询结果语句</param>
  923. /// <returns>查询结果(object)</returns>
  924. public static object GetSingle(string SQLString, params MySqlParameter[] cmdParms)
  925. {
  926. using (MySqlConnection connection = new MySqlConnection(connectionString))
  927. {
  928. using (MySqlCommand cmd = new MySqlCommand())
  929. {
  930. try
  931. {
  932. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  933. object obj = cmd.ExecuteScalar();
  934. cmd.Parameters.Clear();
  935. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  936. {
  937. return null;
  938. }
  939. else
  940. {
  941. return obj;
  942. }
  943. }
  944. catch (MySql.Data.MySqlClient.MySqlException e)
  945. {
  946. throw e;
  947. }
  948. cmd.Dispose();
  949. }
  950. connection.Close();
  951. }
  952. }
  953. /// <summary>
  954. /// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
  955. /// </summary>
  956. /// <param name="strSQL">查询语句</param>
  957. /// <returns>MySqlDataReader</returns>
  958. public static MySqlDataReader ExecuteReader(string SQLString, params MySqlParameter[] cmdParms)
  959. {
  960. MySqlConnection connection = new MySqlConnection(connectionString);
  961. MySqlCommand cmd = new MySqlCommand();
  962. try
  963. {
  964. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  965. MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  966. cmd.Parameters.Clear();
  967. return myReader;
  968. }
  969. catch (MySql.Data.MySqlClient.MySqlException e)
  970. {
  971. throw e;
  972. }
  973. // finally
  974. // {
  975. // cmd.Dispose();
  976. // connection.Close();
  977. // }
  978. }
  979. /// <summary>
  980. /// 执行查询语句,返回DataSet
  981. /// </summary>
  982. /// <param name="SQLString">查询语句</param>
  983. /// <returns>DataSet</returns>
  984. public static DataSet Query(string SQLString, params MySqlParameter[] cmdParms)
  985. {
  986. using (MySqlConnection connection = new MySqlConnection(connectionString))
  987. {
  988. MySqlCommand cmd = new MySqlCommand();
  989. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  990. using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
  991. {
  992. DataSet ds = new DataSet();
  993. try
  994. {
  995. da.Fill(ds, "ds");
  996. cmd.Parameters.Clear();
  997. }
  998. catch (MySql.Data.MySqlClient.MySqlException ex)
  999. {
  1000. throw new Exception(ex.Message);
  1001. }
  1002. return ds;
  1003. }
  1004. }
  1005. }
  1006. private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText, MySqlParameter[] cmdParms)
  1007. {
  1008. if (conn.State != ConnectionState.Open)
  1009. conn.Open();
  1010. cmd.Connection = conn;
  1011. cmd.CommandText = cmdText;
  1012. if (trans != null)
  1013. cmd.Transaction = trans;
  1014. cmd.CommandType = CommandType.Text;//cmdType;
  1015. if (cmdParms != null)
  1016. {
  1017. foreach (MySqlParameter parameter in cmdParms)
  1018. {
  1019. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  1020. (parameter.Value == null))
  1021. {
  1022. parameter.Value = DBNull.Value;
  1023. }
  1024. cmd.Parameters.Add(parameter);
  1025. }
  1026. }
  1027. }
  1028. #endregion
  1029. #region
  1030. /// <summary>
  1031. // @tblName nvarchar(200), ----要显示的表或多个表的连接
  1032. //@fldName nvarchar(500) = '*', ----要显示的字段列表
  1033. //@pageSize int = 1, ----每页显示的记录个数
  1034. //@page int = 10, ----要显示那一页的记录
  1035. //@pageCount int = 1 output, ----查询结果分页后的总页数
  1036. //@Counts int = 1 output, ----查询到的记录数
  1037. //@fldSort nvarchar(200) = null, ----排序字段列表或条件
  1038. //@Sort bit = 1, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
  1039. //@strCondition nvarchar(1000) = null, ----查询条件,不需where
  1040. //@ID nvarchar(150), ----主表的主键
  1041. //@Dist bit = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
  1042. /// </summary>
  1043. /// <param name="tblName"></param>
  1044. /// <param name="fldName"></param>
  1045. /// <param name="pagesize"></param>
  1046. /// <param name="page"></param>
  1047. /// <param name="pagecout"></param>
  1048. /// <param name="counts"></param>
  1049. /// <param name="fldSort"></param>
  1050. /// <param name="Sort"></param>
  1051. /// <param name="strCondition"></param>
  1052. /// <param name="ID"></param>
  1053. /// <param name="Dist"></param>
  1054. /// <returns></returns>
  1055. #endregion
  1056. public static DataTable RunProcedure_GetListPage(
  1057. string tblName,
  1058. string fldName,
  1059. int pagesize,
  1060. int page,
  1061. ref int pagecout,
  1062. ref int counts,
  1063. string fldSort,
  1064. int Sort,
  1065. string strCondition,
  1066. string ID,
  1067. int Dist
  1068. )
  1069. {
  1070. DataTable dt;
  1071. try
  1072. {
  1073. using (MySqlConnection connection = new MySqlConnection(connectionString))
  1074. {
  1075. connection.Open();
  1076. MySqlCommand cmd = new MySqlCommand();
  1077. cmd.Connection = connection;
  1078. cmd.CommandText = "P_ListPage";
  1079. cmd.CommandType = CommandType.StoredProcedure;
  1080. IDataParameter[] parameters = new IDataParameter[] {
  1081. new MySqlParameter("?p_table_name", MySqlDbType.VarChar, 100),
  1082. new MySqlParameter("?p_fields", MySqlDbType.VarChar, 100),
  1083. new MySqlParameter("?p_page_size", MySqlDbType.Int32),
  1084. new MySqlParameter("?p_page_now", MySqlDbType.Int32),
  1085. new MySqlParameter("?p_pagecount", MySqlDbType.Int32),
  1086. new MySqlParameter("?p_totals", MySqlDbType.Int32),
  1087. new MySqlParameter("?p_fldSort", MySqlDbType.VarChar, 200),
  1088. new MySqlParameter("?p_sort", MySqlDbType.Int32),
  1089. new MySqlParameter("?p_where_string", MySqlDbType.VarChar, 200),
  1090. new MySqlParameter("?p_id", MySqlDbType.VarChar, 200)
  1091. };
  1092. parameters[0].Value = tblName;
  1093. parameters[1].Value = fldName;
  1094. parameters[2].Value = pagesize;
  1095. parameters[3].Value = page;
  1096. parameters[4].Direction = ParameterDirection.Output;
  1097. parameters[5].Direction = ParameterDirection.Output;
  1098. parameters[6].Value = fldSort;
  1099. parameters[7].Value = Sort;
  1100. parameters[8].Value = " 1=1 ";
  1101. if (strCondition != null)
  1102. {
  1103. parameters[8].Value = " 1=1 and " + strCondition;
  1104. }
  1105. parameters[9].Value = ID;
  1106. for (int i = 0; i < parameters.Length; i++)
  1107. {
  1108. cmd.Parameters.Add(parameters[i]);
  1109. }
  1110. MySqlDataAdapter dp = new MySqlDataAdapter(cmd);
  1111. DataSet ds = new DataSet();
  1112. dp.Fill(ds);
  1113. pagecout = int.Parse(ds.Tables[1].Rows[0]["p_pagecount"].ToString());
  1114. counts = int.Parse(ds.Tables[1].Rows[0]["p_totals"].ToString());
  1115. dt = ds.Tables[0];
  1116. }
  1117. }
  1118. catch (Exception ex)
  1119. {
  1120. throw ex;
  1121. }
  1122. return dt;
  1123. }
  1124. #region
  1125. /// <summary>
  1126. // @tblName nvarchar(200), ----要显示的表或多个表的连接
  1127. //@fldName nvarchar(500) = '*', ----要显示的字段列表
  1128. //@pageSize int = 1, ----每页显示的记录个数
  1129. //@page int = 10, ----要显示那一页的记录
  1130. //@pageCount int = 1 output, ----查询结果分页后的总页数
  1131. //@Counts int = 1 output, ----查询到的记录数
  1132. //@fldSort nvarchar(200) = null, ----排序字段列表或条件
  1133. //@Sort bit = 1, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
  1134. //@strCondition nvarchar(1000) = null, ----查询条件,不需where
  1135. //@ID nvarchar(150), ----主表的主键
  1136. //@Dist bit = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
  1137. /// </summary>
  1138. /// <param name="tblName"></param>
  1139. /// <param name="fldName"></param>
  1140. /// <param name="pagesize"></param>
  1141. /// <param name="page"></param>
  1142. /// <param name="pagecout"></param>
  1143. /// <param name="counts"></param>
  1144. /// <param name="fldSort"></param>
  1145. /// <param name="Sort"></param>
  1146. /// <param name="strCondition"></param>
  1147. /// <param name="ID"></param>
  1148. /// <param name="Dist"></param>
  1149. /// <returns></returns>
  1150. #endregion
  1151. public static DataTable RunProcedure_GetListPagebySort(
  1152. string tblName,
  1153. string fldName,
  1154. int pagesize,
  1155. int page,
  1156. ref int pagecout,
  1157. ref int counts,
  1158. string p_order_string,
  1159. string strCondition,
  1160. string ID,
  1161. int Dist
  1162. )
  1163. {
  1164. DataTable dt;
  1165. try
  1166. {
  1167. using (MySqlConnection connection = new MySqlConnection(connectionString))
  1168. {
  1169. connection.Open();
  1170. MySqlCommand cmd = new MySqlCommand();
  1171. cmd.Connection = connection;
  1172. cmd.CommandText = "P_ListPagebySort";
  1173. cmd.CommandType = CommandType.StoredProcedure;
  1174. IDataParameter[] parameters = new IDataParameter[] {
  1175. new MySqlParameter("?p_table_name", MySqlDbType.VarChar, 100),
  1176. new MySqlParameter("?p_fields", MySqlDbType.VarChar, 100),
  1177. new MySqlParameter("?p_page_size", MySqlDbType.Int32),
  1178. new MySqlParameter("?p_page_now", MySqlDbType.Int32),
  1179. new MySqlParameter("?p_pagecount", MySqlDbType.Int32),
  1180. new MySqlParameter("?p_totals", MySqlDbType.Int32),
  1181. new MySqlParameter("?p_order_string", MySqlDbType.VarChar, 200),
  1182. new MySqlParameter("?p_where_string", MySqlDbType.VarChar, 200),
  1183. new MySqlParameter("?p_id", MySqlDbType.VarChar, 200)
  1184. };
  1185. parameters[0].Value = tblName;
  1186. parameters[1].Value = fldName;
  1187. parameters[2].Value = pagesize;
  1188. parameters[3].Value = page;
  1189. parameters[4].Direction = ParameterDirection.Output;
  1190. parameters[5].Direction = ParameterDirection.Output;
  1191. parameters[6].Value = p_order_string;
  1192. parameters[7].Value = " 1=1 ";
  1193. if (strCondition != null)
  1194. {
  1195. parameters[7].Value = " 1=1 and " + strCondition;
  1196. }
  1197. parameters[8].Value = ID;
  1198. for (int i = 0; i < parameters.Length; i++)
  1199. {
  1200. cmd.Parameters.Add(parameters[i]);
  1201. }
  1202. MySqlDataAdapter dp = new MySqlDataAdapter(cmd);
  1203. DataSet ds = new DataSet();
  1204. dp.Fill(ds);
  1205. pagecout = int.Parse(ds.Tables[1].Rows[0]["p_pagecount"].ToString());
  1206. counts = int.Parse(ds.Tables[1].Rows[0]["p_totals"].ToString());
  1207. dt = ds.Tables[0];
  1208. }
  1209. }
  1210. catch (Exception ex)
  1211. {
  1212. dt = new DataTable();
  1213. }
  1214. return dt;
  1215. }
  1216. #region
  1217. /// <summary>
  1218. // @tblName nvarchar(200), ----要显示的表或多个表的连接
  1219. //@fldName nvarchar(500) = '*', ----要显示的字段列表
  1220. //@pageSize int = 1, ----每页显示的记录个数
  1221. //@page int = 10, ----要显示那一页的记录
  1222. //@pageCount int = 1 output, ----查询结果分页后的总页数
  1223. //@Counts int = 1 output, ----查询到的记录数
  1224. //@fldSort nvarchar(200) = null, ----排序字段列表或条件
  1225. //@Sort bit = 1, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
  1226. //@strCondition nvarchar(1000) = null, ----查询条件,不需where
  1227. //@ID nvarchar(150), ----主表的主键
  1228. //@Dist bit = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
  1229. /// </summary>
  1230. /// <param name="tblName"></param>
  1231. /// <param name="fldName"></param>
  1232. /// <param name="pagesize"></param>
  1233. /// <param name="page"></param>
  1234. /// <param name="pagecout"></param>
  1235. /// <param name="counts"></param>
  1236. /// <param name="fldSort"></param>
  1237. /// <param name="Sort"></param>
  1238. /// <param name="strCondition"></param>
  1239. /// <param name="ID"></param>
  1240. /// <param name="Dist"></param>
  1241. /// <returns></returns>
  1242. #endregion
  1243. public static DataTable RunProcedure_GetListPagebyGroup(
  1244. string tblName,
  1245. string fldName,
  1246. int pagesize,
  1247. int page,
  1248. ref int pagecout,
  1249. ref int selfcounts,
  1250. ref int counts,
  1251. string p_order_string,
  1252. string strCondition,
  1253. string strgroup,
  1254. string ID,
  1255. int Dist
  1256. )
  1257. {
  1258. DataTable dt;
  1259. try
  1260. {
  1261. using (MySqlConnection connection = new MySqlConnection(connectionString))
  1262. {
  1263. connection.Open();
  1264. MySqlCommand cmd = new MySqlCommand();
  1265. cmd.Connection = connection;
  1266. cmd.CommandText = "P_ListPagebyGroup";
  1267. cmd.CommandType = CommandType.StoredProcedure;
  1268. IDataParameter[] parameters = new IDataParameter[] {
  1269. new MySqlParameter("?p_table_name", MySqlDbType.VarChar, 100),
  1270. new MySqlParameter("?p_fields", MySqlDbType.VarChar, 1000),
  1271. new MySqlParameter("?p_page_size", MySqlDbType.Int32),
  1272. new MySqlParameter("?p_page_now", MySqlDbType.Int32),
  1273. new MySqlParameter("?p_pagecount", MySqlDbType.Int32),
  1274. new MySqlParameter("?p_totals", MySqlDbType.Int32),
  1275. new MySqlParameter("?p_order_string", MySqlDbType.VarChar, 200),
  1276. new MySqlParameter("?p_where_string", MySqlDbType.VarChar, 200),
  1277. new MySqlParameter("?p_group_string", MySqlDbType.VarChar, 200),
  1278. new MySqlParameter("?p_id", MySqlDbType.VarChar, 200)
  1279. };
  1280. parameters[0].Value = tblName;
  1281. parameters[1].Value = fldName;
  1282. parameters[2].Value = pagesize;
  1283. parameters[3].Value = page;
  1284. parameters[4].Direction = ParameterDirection.Output;
  1285. parameters[5].Direction = ParameterDirection.Output;
  1286. parameters[6].Value = p_order_string;
  1287. parameters[7].Value = " 1=1 ";
  1288. if (strCondition != null)
  1289. {
  1290. parameters[7].Value = " 1=1 and " + strCondition;
  1291. }
  1292. if (strgroup != null && strgroup != "")
  1293. {
  1294. parameters[8].Value = strgroup;
  1295. }
  1296. parameters[9].Value = ID;
  1297. for (int i = 0; i < parameters.Length; i++)
  1298. {
  1299. cmd.Parameters.Add(parameters[i]);
  1300. }
  1301. MySqlDataAdapter dp = new MySqlDataAdapter(cmd);
  1302. DataSet ds = new DataSet();
  1303. dp.Fill(ds);
  1304. pagecout = int.Parse(ds.Tables[1].Rows[0]["p_pagecount"].ToString());
  1305. selfcounts = int.Parse(ds.Tables[1].Rows[0]["p_totals"].ToString());
  1306. dt = ds.Tables[0];
  1307. }
  1308. }
  1309. catch (Exception ex)
  1310. {
  1311. dt = new DataTable();
  1312. }
  1313. return dt;
  1314. }
  1315. }
  1316. }