MySqlHelper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. namespace TimedUpload.utils
  9. {
  10. public class MySqlHelper
  11. {
  12. private readonly string connStr;
  13. public MySqlHelper(string configConnStrKey)
  14. {
  15. connStr = ConfigurationManager.AppSettings[configConnStrKey];
  16. }
  17. public DataTable GetDataTable(string sql)
  18. {
  19. MySqlCommand cmd;
  20. MySqlConnection con = new MySqlConnection(connStr);
  21. MySqlDataAdapter msa;
  22. try
  23. {
  24. con.Open();
  25. cmd = new MySqlCommand(sql, con);
  26. msa = new MySqlDataAdapter(cmd);
  27. DataTable dt = new DataTable();
  28. msa.Fill(dt);
  29. con.Close();
  30. return dt;
  31. }
  32. catch (Exception ex)
  33. {
  34. con.Close();
  35. return null;
  36. }
  37. }
  38. public int ExecuteNonQuery(string sql)
  39. {
  40. MySqlCommand cmd;
  41. MySqlConnection con = new MySqlConnection(connStr);
  42. try
  43. {
  44. con.Open();
  45. cmd = new MySqlCommand(sql, con);
  46. int rs = cmd.ExecuteNonQuery();
  47. con.Close();
  48. return rs;
  49. }
  50. catch (Exception ex)
  51. {
  52. con.Close();
  53. return 0;
  54. }
  55. }
  56. }
  57. }