DBConnection.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data.MySqlClient;
  7. using System.Configuration;
  8. namespace parkMonitor.DataBase
  9. {
  10. public class DBConnection
  11. {
  12. /// <summary>
  13. /// 连接字符串
  14. /// </summary>
  15. public static string localStr = "SqlConnectionLocation";
  16. public static string remoteStr = "SqlConnectionStr";
  17. public static string localConf, remoteConf;
  18. public static string localIP, remoteIP;
  19. public static void Init()
  20. {
  21. try
  22. {
  23. localConf = ConfigurationManager.AppSettings[localStr];
  24. remoteConf = ConfigurationManager.AppSettings[remoteStr];
  25. localIP = ConfigurationManager.AppSettings["localDBIP"];
  26. remoteIP = ConfigurationManager.AppSettings["remoteDBIP"];
  27. }
  28. catch
  29. {
  30. Console.WriteLine("配置文件有误");
  31. }
  32. }
  33. /// <summary>
  34. /// 连接句柄
  35. /// </summary>
  36. /// <param name="connectionStr"></param>
  37. /// <returns></returns>
  38. //public MySqlConnection getConn(string connectionStr)
  39. //{
  40. // MySqlConnection con = null;
  41. // try
  42. // {
  43. // if (connectionStr == localStr)
  44. // {
  45. // con = new MySqlConnection(localConf);
  46. // }
  47. // else if (connectionStr == remoteStr)
  48. // {
  49. // con = new MySqlConnection(remoteConf);
  50. // }
  51. // return con;
  52. // }
  53. // catch { }
  54. // return null;
  55. //}
  56. /// <summary>
  57. /// 预处理句柄
  58. /// </summary>
  59. /// <param name="sql"></param>
  60. /// <param name="con"></param>
  61. /// <returns></returns>
  62. public MySqlCommand getComm(string sql, MySqlConnection con)
  63. {
  64. MySqlCommand cmd = null;
  65. try
  66. {
  67. cmd = new MySqlCommand(sql, con);
  68. }
  69. catch (MySqlException)
  70. {
  71. Console.WriteLine("数据库连接异常");
  72. }
  73. catch
  74. {
  75. Console.WriteLine("sql语句异常");
  76. }
  77. return cmd;
  78. }
  79. }
  80. }