AccsessDbLoader.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.OleDb;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using GWSocketClient.model;
  10. using Newtonsoft.Json;
  11. namespace GWSocketClient.db
  12. {
  13. class AccsessDbLoader
  14. {
  15. private static AccsessDbLoader INSTANCE = null;
  16. private DataTable dataTable = new DataTable();
  17. private OleDbDataAdapter dataAdapter;
  18. private List<WvBean> wvBeans = new List<WvBean>();
  19. public Dictionary<string, string> ID_INFO = new Dictionary<string, string>();
  20. public static AccsessDbLoader getInstance(string dbssPath = "")
  21. {
  22. return INSTANCE ?? (INSTANCE = new AccsessDbLoader(dbssPath));
  23. }
  24. public DataTable getDataTable()
  25. {
  26. return dataTable;
  27. }
  28. public OleDbDataAdapter getDataAdapter()
  29. {
  30. return dataAdapter;
  31. }
  32. public Dictionary<string, string> getUserIdInfo()
  33. {
  34. return ID_INFO;
  35. }
  36. public string getWebViewJson()
  37. {
  38. return JsonConvert.SerializeObject(wvBeans);
  39. }
  40. private AccsessDbLoader(string dbPath = "")
  41. {
  42. if (dbPath.Equals(""))
  43. {
  44. dbPath = Application.StartupPath + "/dbfile/ID_INFO.accdb;";
  45. }
  46. reloadAccessDb(dbPath);
  47. }
  48. public AccsessDbLoader reloadAccessDb(string dbPath)
  49. {
  50. OleDbConnection mycon = null;
  51. OleDbDataReader myReader = null;
  52. OleDbCommand sqlcmd = null;
  53. string strConnection = "Provider = Microsoft.ACE.OLEDB.12.0;" + "Data Source = " + dbPath;
  54. try
  55. {
  56. using (OleDbConnection objConnection = new OleDbConnection(strConnection))
  57. {
  58. objConnection.Open();
  59. sqlcmd = new OleDbCommand(@"select * from fw", objConnection); //sql语句
  60. dataAdapter = new OleDbDataAdapter(sqlcmd);
  61. dataAdapter.Fill(dataTable);
  62. using (OleDbDataReader reader = sqlcmd.ExecuteReader())
  63. {
  64. if (reader.Read()) //这个read调用很重要!不写的话运行时将提示找不到数据
  65. {
  66. WvBean wb = new WvBean();
  67. int id = 0;
  68. int index = 0;
  69. string myId = "";
  70. string idInfo = "";
  71. string len = "";
  72. string mode = "";
  73. string desp = "";
  74. string defalutValue = "";
  75. string userValue = "";
  76. string remark = "";
  77. try
  78. {
  79. id = reader.GetInt32(0);
  80. index = reader.GetInt32(1);
  81. myId = reader.GetString(2);
  82. idInfo = calcNull(reader.GetString(3));
  83. len = calcNull(reader.GetString(4));
  84. mode = calcNull(reader.GetString(5));
  85. desp = calcNull(reader.GetString(6));
  86. defalutValue = calcNull(reader.GetString(7));
  87. userValue = calcNull(reader.GetString(8));
  88. remark = calcNull(reader.GetString(9));
  89. }
  90. catch (Exception exception)
  91. {
  92. }
  93. wb.id = id;
  94. wb.index = index;
  95. wb.myId = myId;
  96. wb.idInfo = idInfo;
  97. wb.len = len;
  98. wb.mode = mode;
  99. wb.desp = desp;
  100. wb.defalutValue = defalutValue;
  101. wb.userValue = userValue;
  102. wb.remark = remark;
  103. ID_INFO.Add(myId, userValue);
  104. wvBeans.Add(wb);
  105. }
  106. }
  107. }
  108. return this;
  109. }
  110. catch (OleDbException oe)
  111. {
  112. MessageBox.Show(oe.Message);
  113. }
  114. catch (Exception exception)
  115. {
  116. MyMessageBox.ShowMessageBox(exception.Message);
  117. }
  118. return null;
  119. }
  120. private string calcNull(string ojb)
  121. {
  122. if (ojb.Trim().Equals("") || ojb == null)
  123. {
  124. return "";
  125. }
  126. return ojb;
  127. }
  128. }
  129. }