123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.OleDb;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using GWSocketClient.model;
- using Newtonsoft.Json;
-
- namespace GWSocketClient.db
- {
- class AccsessDbLoader
- {
- private static AccsessDbLoader INSTANCE = null;
-
- private DataTable dataTable = new DataTable();
- private OleDbDataAdapter dataAdapter;
-
- private List<WvBean> wvBeans = new List<WvBean>();
- public Dictionary<string, string> ID_INFO = new Dictionary<string, string>();
-
- public static AccsessDbLoader getInstance(string dbssPath = "")
- {
- return INSTANCE ?? (INSTANCE = new AccsessDbLoader(dbssPath));
- }
-
- public DataTable getDataTable()
- {
- return dataTable;
- }
-
-
- public OleDbDataAdapter getDataAdapter()
- {
- return dataAdapter;
- }
-
- public Dictionary<string, string> getUserIdInfo()
- {
- return ID_INFO;
- }
-
- public string getWebViewJson()
- {
- return JsonConvert.SerializeObject(wvBeans);
- }
-
- private AccsessDbLoader(string dbPath = "")
- {
- if (dbPath.Equals(""))
- {
- dbPath = Application.StartupPath + "/dbfile/ID_INFO.accdb;";
- }
- reloadAccessDb(dbPath);
- }
-
-
- public AccsessDbLoader reloadAccessDb(string dbPath)
- {
- OleDbConnection mycon = null;
- OleDbDataReader myReader = null;
-
- OleDbCommand sqlcmd = null;
-
- string strConnection = "Provider = Microsoft.ACE.OLEDB.12.0;" + "Data Source = " + dbPath;
-
- try
- {
- using (OleDbConnection objConnection = new OleDbConnection(strConnection))
- {
- objConnection.Open();
- sqlcmd = new OleDbCommand(@"select * from fw", objConnection); //sql语句
-
-
- dataAdapter = new OleDbDataAdapter(sqlcmd);
-
- dataAdapter.Fill(dataTable);
-
- using (OleDbDataReader reader = sqlcmd.ExecuteReader())
- {
- if (reader.Read()) //这个read调用很重要!不写的话运行时将提示找不到数据
- {
- WvBean wb = new WvBean();
- int id = 0;
- int index = 0;
- string myId = "";
- string idInfo = "";
- string len = "";
- string mode = "";
- string desp = "";
- string defalutValue = "";
- string userValue = "";
- string remark = "";
- try
- {
- id = reader.GetInt32(0);
- index = reader.GetInt32(1);
- myId = reader.GetString(2);
- idInfo = calcNull(reader.GetString(3));
- len = calcNull(reader.GetString(4));
- mode = calcNull(reader.GetString(5));
- desp = calcNull(reader.GetString(6));
- defalutValue = calcNull(reader.GetString(7));
- userValue = calcNull(reader.GetString(8));
- remark = calcNull(reader.GetString(9));
- }
- catch (Exception exception)
- {
- }
-
- wb.id = id;
- wb.index = index;
- wb.myId = myId;
- wb.idInfo = idInfo;
- wb.len = len;
- wb.mode = mode;
- wb.desp = desp;
- wb.defalutValue = defalutValue;
- wb.userValue = userValue;
- wb.remark = remark;
-
- ID_INFO.Add(myId, userValue);
-
- wvBeans.Add(wb);
- }
- }
- }
- return this;
- }
- catch (OleDbException oe)
- {
- MessageBox.Show(oe.Message);
- }
- catch (Exception exception)
- {
- MyMessageBox.ShowMessageBox(exception.Message);
- }
-
- return null;
- }
-
- private string calcNull(string ojb)
- {
- if (ojb.Trim().Equals("") || ojb == null)
- {
- return "";
- }
- return ojb;
- }
- }
- }
|