MySQL.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace BaseLibDataProcess
  12. {
  13. public class ClsMysql
  14. {
  15. private readonly string IP = null;
  16. private readonly string User = null;
  17. private readonly string Password = null;
  18. private readonly string dbName = null;
  19. private readonly string Port = null;
  20. private readonly string Charset = "utf8";
  21. public ClsMysql(string ip, string port, string user, string pwd, string dbname,string charset= "utf8")
  22. {
  23. IP = ip;
  24. Port = port;
  25. User = user;
  26. Password = pwd;
  27. dbName = dbname;
  28. Charset = charset;
  29. }
  30. public bool CreateDataBase()
  31. {
  32. try
  33. {
  34. if (!ExistDatabase(dbName.ToLower()))
  35. {
  36. if (Create_database(dbName.ToLower()))
  37. {
  38. return true;
  39. }
  40. }
  41. else
  42. {
  43. return true;
  44. }
  45. return false;
  46. }
  47. catch (Exception ex)
  48. {
  49. MessageBox.Show($"创建数据库{dbName}失败\r\nFailed to create database\r\n{ex.Message}", "Error");
  50. return false;
  51. }
  52. }
  53. public MySqlConnection GetMySqlConnector(string m_userid, string m_password)
  54. {
  55. //string strcon = string.Format("Data Source={2};Persist Security Info=yes;UserId={0}; PWD={1};port={3}", m_userid, m_password, IP, Port,dbName);
  56. string strcon = $"Data Source={IP};Persist Security Info=yes;UserId={m_userid}; PWD={m_password};port={Port}";
  57. MySqlConnection con = new MySqlConnection(strcon);
  58. return con;
  59. }
  60. public bool ExistDatabase(string basename)
  61. {
  62. bool flag = false;
  63. MySqlConnection con = null;
  64. MySqlCommand cmd = null;
  65. MySqlDataReader dr = null;
  66. StringBuilder str_builder = new StringBuilder();
  67. string str = "select * from information_schema.SCHEMATA where SCHEMA_NAME=";
  68. str_builder.Append(str);
  69. str_builder.Append("'");
  70. str_builder.Append(basename);
  71. str_builder.Append("'");
  72. string sql_str = str_builder.ToString();
  73. try
  74. {
  75. con = GetMySqlConnector(User, Password);
  76. if (con.State == ConnectionState.Closed)
  77. {
  78. con.Open();
  79. }
  80. cmd = new MySqlCommand(sql_str, con);
  81. dr = cmd.ExecuteReader();
  82. while (dr.Read())
  83. {
  84. if (dr.GetString(1).ToString() == basename)
  85. {
  86. flag = true;
  87. break;
  88. }
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. MessageBox.Show($"{ex.Message}", "Database Error");
  94. }
  95. finally
  96. {
  97. if (cmd != null)
  98. {
  99. cmd.Dispose();
  100. }
  101. if (con != null && con.State != ConnectionState.Open)
  102. {
  103. con.Close();
  104. }
  105. con.Dispose();
  106. }
  107. return flag;
  108. }
  109. public bool Create_database(string basename)
  110. {
  111. bool result = false;
  112. MySqlConnection con = null;
  113. MySqlCommand cmd = null;
  114. string sql_str = "Create Database " + basename;
  115. try
  116. {
  117. con = GetMySqlConnector(User, Password);
  118. if (con.State == ConnectionState.Closed)
  119. {
  120. con.Open();
  121. }
  122. cmd = new MySqlCommand(sql_str, con);
  123. cmd.ExecuteNonQuery();
  124. result = true;
  125. }
  126. catch (Exception ex)
  127. {
  128. MessageBox.Show($"创建数据库失败\r\n{ex.Message}","Error");
  129. result = false;
  130. }
  131. finally
  132. {
  133. if (cmd != null)
  134. {
  135. cmd.Dispose();
  136. }
  137. if (con != null && con.State != ConnectionState.Open)
  138. {
  139. con.Close();
  140. }
  141. con.Dispose();
  142. }
  143. return result;
  144. }
  145. public MySqlConnection GetMySqlConnector()
  146. {
  147. string m_str_con = string.Format("server={0};user id={1}; password={2};database={3};port={4};charset={5}", IP, User, Password, dbName, Port, Charset);
  148. MySqlConnection con = new MySqlConnection(m_str_con);
  149. return con;
  150. }
  151. public bool DeleteRow(string strTable,string strItem,string strValue)
  152. {
  153. bool result = false;
  154. MySqlConnection con = null;
  155. MySqlCommand cmd = null;
  156. try
  157. {
  158. StringBuilder str_builder = new StringBuilder();
  159. str_builder.Append("delete from ");
  160. str_builder.Append(strTable);
  161. str_builder.Append($" where {strItem}=");
  162. str_builder.Append("'");
  163. str_builder.Append(strValue);
  164. str_builder.Append("'");
  165. con = GetMySqlConnector();
  166. if (con.State == ConnectionState.Closed)
  167. {
  168. con.Open();
  169. }
  170. cmd = new MySqlCommand(str_builder.ToString(), con);
  171. cmd.ExecuteNonQuery();
  172. result = true;
  173. }
  174. catch(Exception ex)
  175. {
  176. MessageBox.Show($"删除数据库数据失败\r\nFailed to delete the data of database\r\n{ex.Message}", "Error");
  177. }
  178. finally
  179. {
  180. if (cmd != null)
  181. {
  182. cmd.Dispose();
  183. }
  184. if (con != null && con.State != ConnectionState.Open)
  185. {
  186. con.Close();
  187. }
  188. con.Dispose();
  189. }
  190. return result;
  191. }
  192. public int Getrecords(string m_str)
  193. {
  194. int result = -1;
  195. MySqlConnection con = null;
  196. MySqlCommand cmd = null;
  197. try
  198. {
  199. con = GetMySqlConnector();
  200. if (con.State == ConnectionState.Closed)
  201. {
  202. con.Open();
  203. }
  204. cmd = new MySqlCommand(m_str, con);
  205. result = Convert.ToInt32(cmd.ExecuteScalar());
  206. }
  207. catch
  208. {
  209. }
  210. finally
  211. {
  212. if (cmd != null)
  213. {
  214. cmd.Dispose();
  215. }
  216. if (con != null && con.State != ConnectionState.Open)
  217. {
  218. con.Close();
  219. }
  220. con.Dispose();
  221. }
  222. return result;
  223. }
  224. //public bool InsertData(string tablename, Dictionary<int, ContentInfo> _dic)
  225. //{
  226. // bool result = false;
  227. // MySqlConnection con = null;
  228. // MySqlCommand cmd = null;
  229. // try
  230. // {
  231. // int m = _dic.Count;
  232. // StringBuilder str_builder = new StringBuilder();
  233. // str_builder.Append("Insert into ");
  234. // str_builder.Append(tablename);
  235. // str_builder.Append(" (");
  236. // for (int i = 0; i < m; i++)
  237. // {
  238. // ContentInfo m_contentInfo = null;
  239. // if (_dic.TryGetValue(i, out m_contentInfo))
  240. // {
  241. // str_builder.Append(m_contentInfo.str_field);
  242. // if (i < (m - 1))
  243. // {
  244. // str_builder.Append(",");
  245. // }
  246. // if (i == (m - 1))
  247. // {
  248. // str_builder.Append(") ");
  249. // }
  250. // }
  251. // }
  252. // str_builder.Append(" values( ");
  253. // for (int i = 0; i < m; i++)
  254. // {
  255. // ContentInfo m_contentInfo = null;
  256. // if (_dic.TryGetValue(i, out m_contentInfo))
  257. // {
  258. // str_builder.Append("'");
  259. // str_builder.Append(m_contentInfo.obj_content);
  260. // str_builder.Append("'");
  261. // if (i == (m - 1))
  262. // {
  263. // str_builder.Append(" )");
  264. // }
  265. // if (i < (m - 1))
  266. // {
  267. // str_builder.Append(",");
  268. // }
  269. // }
  270. // }
  271. // string m_str = str_builder.ToString();
  272. // con = GetMySqlConnector();
  273. // if (con.State == ConnectionState.Closed)
  274. // {
  275. // con.Open();
  276. // }
  277. // cmd = new MySqlCommand(m_str, con);
  278. // cmd.ExecuteNonQuery();
  279. // result = true;
  280. // }
  281. // catch (Exception ex)
  282. // {
  283. // MessageBox.Show($"插入数据到数据库失败{ex.Message}", "Error");
  284. // // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:InsertData");
  285. // }
  286. // finally
  287. // {
  288. // if (cmd != null)
  289. // {
  290. // cmd.Dispose();
  291. // }
  292. // if (con != null && con.State != ConnectionState.Open)
  293. // {
  294. // con.Close();
  295. // }
  296. // con.Dispose();
  297. // }
  298. // return result;
  299. //}
  300. public bool InsertData(string tablename, string strItem,string strValue)
  301. {
  302. bool result = false;
  303. MySqlConnection con = null;
  304. MySqlCommand cmd = null;
  305. try
  306. {
  307. StringBuilder str_builder = new StringBuilder();
  308. str_builder.Append("Insert into ");
  309. str_builder.Append(tablename);
  310. str_builder.Append(" (");
  311. str_builder.Append(strItem);
  312. str_builder.Append(") ");
  313. str_builder.Append(" values( ");
  314. for (int i = 0; i < strValue.Split(',').Length; i++)
  315. {
  316. if (i== strValue.Split(',').Length-1)
  317. {
  318. str_builder.Append($"'{strValue.Split(',')[i]}'");
  319. }
  320. else
  321. {
  322. str_builder.Append($"'{strValue.Split(',')[i]}',");
  323. }
  324. }
  325. str_builder.Append(" )");
  326. string m_str = str_builder.ToString();
  327. con = GetMySqlConnector();
  328. if (con.State == ConnectionState.Closed)
  329. {
  330. con.Open();
  331. }
  332. cmd = new MySqlCommand(m_str, con);
  333. cmd.ExecuteNonQuery();
  334. result = true;
  335. }
  336. catch (Exception ex)
  337. {
  338. MessageBox.Show($"插入数据到数据库失败\r\nFailed to insert data to database\r\n{ex.Message}", "Error");
  339. // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:InsertData");
  340. }
  341. finally
  342. {
  343. if (cmd != null)
  344. {
  345. cmd.Dispose();
  346. }
  347. if (con != null && con.State != ConnectionState.Open)
  348. {
  349. con.Close();
  350. }
  351. con.Dispose();
  352. }
  353. return result;
  354. }
  355. //public bool UpdateData(string tablename, Dictionary<int, ContentInfo> _dic, ContentInfo _condition)
  356. //{
  357. // bool result = false;
  358. // MySqlConnection con = null;
  359. // MySqlCommand cmd = null;
  360. // try
  361. // {
  362. // int m = _dic.Count;
  363. // StringBuilder str_builder = new StringBuilder();
  364. // str_builder.Append("update ");
  365. // str_builder.Append(tablename);
  366. // str_builder.Append(" set ");
  367. // for (int i = 0; i < m; i++)
  368. // {
  369. // ContentInfo m_contentInfo = null;
  370. // if (_dic.TryGetValue(i, out m_contentInfo))
  371. // {
  372. // str_builder.Append(m_contentInfo.str_field);
  373. // str_builder.Append("=");
  374. // str_builder.Append("'");
  375. // str_builder.Append(m_contentInfo.obj_content);
  376. // str_builder.Append("'");
  377. // if (i < (m - 1))
  378. // {
  379. // str_builder.Append(",");
  380. // }
  381. // }
  382. // }
  383. // str_builder.Append(" where ");
  384. // str_builder.Append(_condition.str_field);
  385. // str_builder.Append("=");
  386. // str_builder.Append("'");
  387. // str_builder.Append(_condition.obj_content);
  388. // str_builder.Append("'");
  389. // string m_str = str_builder.ToString();
  390. // con = GetMySqlConnector();
  391. // if (con.State == ConnectionState.Closed)
  392. // {
  393. // con.Open();
  394. // }
  395. // cmd = new MySqlCommand(m_str, con);
  396. // cmd.ExecuteNonQuery();
  397. // result = true;
  398. // }
  399. // catch
  400. // {
  401. // // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:UpdateData");
  402. // }
  403. // finally
  404. // {
  405. // if (cmd != null)
  406. // {
  407. // cmd.Dispose();
  408. // }
  409. // if (con != null && con.State != ConnectionState.Open)
  410. // {
  411. // con.Close();
  412. // }
  413. // con.Dispose();
  414. // }
  415. // return result;
  416. //}
  417. public bool UpdateData(string tablename, string strItem,string strValue, string strKeyItem,string strKeyValue)
  418. {
  419. if (strItem.Split(',').Length!= strValue.Split(',').Length)
  420. {
  421. MessageBox.Show("更新数据库数据出错\r\nFailed to update database", "Error");
  422. }
  423. bool result = false;
  424. MySqlConnection con = null;
  425. MySqlCommand cmd = null;
  426. try
  427. {
  428. StringBuilder str_builder = new StringBuilder();
  429. str_builder.Append("update ");
  430. str_builder.Append(tablename);
  431. str_builder.Append(" set ");
  432. for (int i = 0; i < strItem.Split(',').Length; i++)
  433. {
  434. str_builder.Append(strItem.Split(',')[i]);
  435. str_builder.Append("=");
  436. str_builder.Append("'");
  437. str_builder.Append(strValue.Split(',')[i]);
  438. str_builder.Append("'");
  439. if (i < (strItem.Split(',').Length - 1))
  440. {
  441. str_builder.Append(",");
  442. }
  443. }
  444. str_builder.Append(" where ");
  445. str_builder.Append(strKeyItem);
  446. str_builder.Append("=");
  447. str_builder.Append("'");
  448. str_builder.Append(strKeyValue);
  449. str_builder.Append("'");
  450. string m_str = str_builder.ToString();
  451. con = GetMySqlConnector();
  452. if (con.State == ConnectionState.Closed)
  453. {
  454. con.Open();
  455. }
  456. cmd = new MySqlCommand(m_str, con);
  457. cmd.ExecuteNonQuery();
  458. result = true;
  459. }
  460. catch(Exception ex)
  461. {
  462. MessageBox.Show($"更新数据库数据出错\r\nFailed to update database\r\n{ex.Message}", "Error");
  463. }
  464. finally
  465. {
  466. if (cmd != null)
  467. {
  468. cmd.Dispose();
  469. }
  470. if (con != null && con.State != ConnectionState.Open)
  471. {
  472. con.Close();
  473. }
  474. con.Dispose();
  475. }
  476. return result;
  477. }
  478. public DataTable InquireData(string strTable,string strItem,string strValue)
  479. {
  480. MySqlConnection con = null;
  481. MySqlCommand cmd = null;
  482. MySqlDataReader dr = null;
  483. DataTable dt = new DataTable();
  484. try
  485. {
  486. StringBuilder str_builder = new StringBuilder();
  487. str_builder.Append("select * from ");
  488. str_builder.Append(strTable);
  489. str_builder.Append($" where {strItem}=");
  490. str_builder.Append("'");
  491. str_builder.Append(strValue);
  492. str_builder.Append("'");
  493. con = GetMySqlConnector();
  494. if (con.State == ConnectionState.Closed)
  495. {
  496. con.Open();
  497. }
  498. cmd = new MySqlCommand(str_builder.ToString(), con);
  499. dr = cmd.ExecuteReader();
  500. for (int i = 0; i < dr.FieldCount; i++)
  501. {
  502. dt.Columns.Add(dr.GetName(i));
  503. }
  504. while (dr.HasRows && dr.Read())
  505. {
  506. DataRow datarow = dt.Rows.Add();
  507. for (int j = 0; j < dt.Columns.Count; j++)
  508. {
  509. datarow[j] = dr[j];
  510. }
  511. }
  512. }
  513. catch
  514. {
  515. // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:ExeQuery");
  516. }
  517. finally
  518. {
  519. if (dr != null)
  520. {
  521. dr.Dispose();
  522. }
  523. if (con.State == ConnectionState.Open)
  524. {
  525. con.Close();
  526. }
  527. if (cmd != null)
  528. {
  529. cmd.Dispose();
  530. }
  531. if (dt != null)
  532. {
  533. dt.Dispose();
  534. }
  535. con.Dispose();
  536. }
  537. return dt;
  538. }
  539. public bool CreateTable(string str_tablename, string[] strfields, ushort keyIndex,ushort timeIndex=0)
  540. {
  541. MySqlConnection con = null;
  542. MySqlCommand cmd = null;
  543. try
  544. {
  545. StringBuilder str_builder = new StringBuilder();
  546. str_builder.Append("Create table if not exists ");
  547. str_builder.Append(str_tablename);
  548. str_builder.Append(" ( ");
  549. for (int i = 0; i < strfields.Length; i++)
  550. {
  551. str_builder.Append(strfields[i]);
  552. str_builder.Append(" ");
  553. if (i == keyIndex)
  554. {
  555. str_builder.Append("varchar(100) character set utf8 primary key ");
  556. }
  557. else if (i == timeIndex)
  558. {
  559. str_builder.Append("datetime ");
  560. }
  561. else
  562. {
  563. str_builder.Append("varchar(100) character set utf8");
  564. }
  565. if (i == strfields.Length - 1)
  566. {
  567. str_builder.Append(" )");
  568. }
  569. else
  570. {
  571. str_builder.Append(", ");
  572. }
  573. }
  574. con = GetMySqlConnector();
  575. if (con.State == ConnectionState.Closed)
  576. {
  577. con.Open();
  578. }
  579. cmd = new MySqlCommand(str_builder.ToString(), con);
  580. cmd.ExecuteNonQuery();
  581. return true;
  582. // str_builder.Append("(MyRepairOrderID int auto_increment primary key,");
  583. //str_builder.Append("RepairOrderCode varchar(100) character set utf8 unique ,");
  584. //str_builder.Append("OrderCode varchar(100) character set utf8 not null,");
  585. //str_builder.Append("approvedname varchar(100) character set utf8,");
  586. //str_builder.Append("sendname varchar(100) character set utf8,");
  587. //str_builder.Append("state varchar(100) character set utf8,");
  588. //str_builder.Append("standnum varchar(100) not null,");
  589. //str_builder.Append("m_date date not null,");
  590. //str_builder.Append("lineside int");
  591. //str_builder.Append(" )");
  592. }
  593. catch (Exception ex)
  594. {
  595. MessageBox.Show($"创建数据库表格失败{ex.Message}", "Database Error");
  596. return false;
  597. }
  598. }
  599. public bool ExistTable(string tablename)
  600. {
  601. // string str = "select table_name from information_schema.tables where table_name='table_process'";
  602. StringBuilder str_builder = new StringBuilder();
  603. str_builder.Append("select table_name from information_schema.tables where table_name=");
  604. str_builder.Append("'");
  605. str_builder.Append(tablename);
  606. str_builder.Append("' and Table_SCHEMA='");
  607. str_builder.Append(dbName);
  608. str_builder.Append("'");
  609. string m_sql = str_builder.ToString();
  610. bool FLAG = false;
  611. MySqlConnection con = null;
  612. MySqlCommand cmd = null;
  613. MySqlDataReader dr = null;
  614. try
  615. {
  616. con = GetMySqlConnector();
  617. if (con.State == ConnectionState.Closed)
  618. {
  619. con.Open();
  620. }
  621. cmd = new MySqlCommand(m_sql, con);
  622. dr = cmd.ExecuteReader();
  623. if (dr.Read() && dr.HasRows)
  624. {
  625. if (dr["table_name"].ToString() == tablename.ToLower())
  626. FLAG = true;
  627. }
  628. }
  629. catch
  630. {
  631. //Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:IS_EXIST_TABLE");
  632. FLAG = false;
  633. }
  634. finally
  635. {
  636. if (con.State == ConnectionState.Open)
  637. {
  638. con.Close();
  639. }
  640. if (cmd != null)
  641. {
  642. cmd.Dispose();
  643. }
  644. if (dr != null)
  645. {
  646. dr.Dispose();
  647. }
  648. con.Dispose();
  649. }
  650. return FLAG;
  651. }
  652. public bool ExistField(string tablename, string field)
  653. {
  654. bool result = false;
  655. MySqlConnection con = null;
  656. MySqlCommand cmd = null;
  657. MySqlDataReader dr = null;
  658. try
  659. {
  660. if (ExistTable(tablename))
  661. {
  662. StringBuilder str_builder = new StringBuilder();
  663. str_builder.Append("describe ");
  664. str_builder.Append(tablename);
  665. string m_sql = str_builder.ToString();
  666. con = GetMySqlConnector();
  667. if (con.State == ConnectionState.Closed)
  668. {
  669. con.Open();
  670. }
  671. cmd = new MySqlCommand(m_sql, con);
  672. dr = cmd.ExecuteReader();
  673. while (dr.Read())
  674. {
  675. if (dr.GetString("Field").ToString() != null)
  676. {
  677. if (dr.GetString("Field").ToString() == field)
  678. {
  679. result = true;
  680. break;
  681. }
  682. }
  683. }
  684. }
  685. }
  686. catch
  687. {
  688. // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:IS_EXIST_FILED");
  689. }
  690. finally
  691. {
  692. if (con.State == ConnectionState.Open)
  693. {
  694. con.Close();
  695. }
  696. if (cmd != null)
  697. {
  698. cmd.Dispose();
  699. }
  700. if (dr != null)
  701. {
  702. dr.Dispose();
  703. }
  704. con.Dispose();
  705. }
  706. return result;
  707. }
  708. public bool Add_Column(string tablename, string field)
  709. {
  710. bool result = false;
  711. MySqlConnection con = null;
  712. MySqlCommand cmd = null;
  713. StringBuilder str_builder = new StringBuilder();
  714. str_builder.Append("alter table ");
  715. str_builder.Append(tablename);
  716. str_builder.Append(" add Column ");
  717. str_builder.Append(field);
  718. str_builder.Append(" varchar(100)");
  719. string m_sql = str_builder.ToString();
  720. try
  721. {
  722. con = GetMySqlConnector();
  723. if (con.State == ConnectionState.Closed)
  724. {
  725. con.Open();
  726. }
  727. cmd = new MySqlCommand(m_sql, con);
  728. cmd.ExecuteNonQuery();
  729. result = true;
  730. }
  731. catch
  732. {
  733. // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:Add_Column");
  734. }
  735. finally
  736. {
  737. if (cmd != null)
  738. {
  739. cmd.Dispose();
  740. }
  741. if (con != null && con.State != ConnectionState.Open)
  742. {
  743. con.Close();
  744. }
  745. con.Dispose();
  746. }
  747. return result;
  748. }
  749. public DataTable GetDataBaseDT(string dtname)
  750. {
  751. DataTable dt = new DataTable();
  752. MySqlConnection con = null;
  753. MySqlDataAdapter sda = null;
  754. string sql_str = "select* from "+ dtname;
  755. try
  756. {
  757. con = GetMySqlConnector();
  758. if (con.State == ConnectionState.Closed)
  759. {
  760. con.Open();
  761. }
  762. sda = new MySqlDataAdapter(sql_str, con);
  763. sda.Fill(dt);
  764. return dt;
  765. }
  766. catch (Exception ex)
  767. {
  768. MessageBox.Show($"获取数据库数据失败{ex.Message}", "Error");
  769. return null;
  770. }
  771. finally
  772. {
  773. if (sda != null)
  774. {
  775. sda.Dispose();
  776. }
  777. if (con != null && con.State != ConnectionState.Open)
  778. {
  779. con.Close();
  780. }
  781. con.Dispose();
  782. }
  783. }
  784. public void Get_recordsCount(ref int m, string tablename, ContentInfo m_contentinfo)
  785. {
  786. if (ExistTable(tablename))
  787. {
  788. StringBuilder str_builder = new StringBuilder();
  789. str_builder.Append("select count(*) from ");
  790. str_builder.Append(tablename);
  791. if (m_contentinfo != null)
  792. {
  793. str_builder.Append(" where ");
  794. str_builder.Append(m_contentinfo.str_field);
  795. str_builder.Append("=");
  796. str_builder.Append("'");
  797. str_builder.Append(m_contentinfo.obj_content);
  798. str_builder.Append("'");
  799. }
  800. string m_sql = str_builder.ToString();
  801. MySqlConnection con = null;
  802. MySqlCommand cmd = null;
  803. try
  804. {
  805. con = GetMySqlConnector();
  806. if (con.State == ConnectionState.Closed)
  807. {
  808. con.Open();
  809. }
  810. cmd = new MySqlCommand(m_sql, con);
  811. m = Convert.ToInt32(cmd.ExecuteScalar());
  812. }
  813. catch
  814. {
  815. // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:Get_recordsCount");
  816. }
  817. finally
  818. {
  819. if (cmd != null)
  820. {
  821. cmd.Dispose();
  822. }
  823. if (con != null && con.State != ConnectionState.Open)
  824. {
  825. con.Close();
  826. }
  827. con.Dispose();
  828. }
  829. }
  830. }
  831. }
  832. public class ContentInfo
  833. {
  834. public string str_field;
  835. public object obj_content;
  836. }
  837. }