DBTest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DatabaseDLL;
  7. using System.Configuration;
  8. using MySql.Data.MySqlClient;
  9. namespace Test
  10. {
  11. class DBTest
  12. {
  13. public string connectionString { get; set; }
  14. public int timeout { get; set; }
  15. public int garageID { get; set; }
  16. public DBOperation oper { get; set; }
  17. public void Init()
  18. {
  19. try
  20. {
  21. connectionString = ConfigurationManager.AppSettings["connectionString"];
  22. timeout = Convert.ToInt32(ConfigurationManager.AppSettings["timeout"]);
  23. garageID = Convert.ToInt32(ConfigurationManager.AppSettings["garageID"]);
  24. }
  25. catch (Exception ex)
  26. {
  27. Console.WriteLine(ex.Message);
  28. }
  29. oper = new DBOperation(connectionString, timeout);
  30. bool flag = oper.InitConnPooling(10);
  31. if (!flag)
  32. {
  33. Console.WriteLine("数据库初始化失败");
  34. }
  35. }
  36. public void Query()
  37. {
  38. string sql = "select * from garage where garageID = '" + garageID + "'";
  39. MySqlDataReader reader = oper.Query(sql);
  40. int count;
  41. if (reader != null)
  42. {
  43. try
  44. {
  45. while (reader.Read())
  46. {
  47. count = reader.GetInt32("garageFreeSpace");
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine(ex.Message);
  53. }
  54. reader.Dispose();
  55. }
  56. }
  57. public void Insert()
  58. {
  59. List<string> list = new List<string>();
  60. string sql = "insert into messagequeue(userID,context,messageType) values(1,'test',1)";
  61. list.Add(sql);
  62. bool flag = oper.Insert(list);
  63. }
  64. public void Update()
  65. {
  66. List<string> list = new List<string>();
  67. string sql = "update messagequeue set messageType = 2 where messageID = 1";
  68. list.Add(sql);
  69. bool flag = oper.UpdateTransaction(list);
  70. }
  71. public bool IsAdmin(string nickname, string password)
  72. {
  73. bool flag = false;
  74. string sql = "select * from manager where managerTel = '" + nickname + "'";
  75. MySqlDataReader reader = oper.Query(sql);
  76. if (reader != null)
  77. {
  78. try
  79. {
  80. while (reader.Read())
  81. {
  82. string managerPassword = reader.GetString("managerPassword");
  83. if (nickname.Equals(reader.GetString("managerTel")) && password.Equals(managerPassword))
  84. {
  85. flag = true;
  86. }
  87. }
  88. reader.Dispose();
  89. }
  90. catch (Exception ex)
  91. {
  92. Console.WriteLine(ex.Message);
  93. }
  94. reader.Dispose();
  95. }
  96. return flag;
  97. }
  98. }
  99. }