AccessDBHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.Windows;
  8. namespace UAS_MES.DataOperate
  9. {
  10. class AccessDBHelper
  11. {
  12. private string _fileName;
  13. private string _connectionString;
  14. private OleDbConnection _odcConnection = null;
  15. public AccessDBHelper(string fileName)
  16. {
  17. this._fileName = fileName;
  18. //this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";
  19. this._connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";";
  20. }
  21. public AccessDBHelper(string fileName,string password)
  22. {
  23. this._fileName = fileName;
  24. //this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Jet OLEDB:Database Password="+password+";";
  25. this._connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Jet OLEDB:Database Password=" + password + ";";
  26. }
  27. public void Open()
  28. {
  29. try
  30. {
  31. // 建立连接
  32. this._odcConnection = new OleDbConnection(this._connectionString);
  33. // 打开连接
  34. this._odcConnection.Open();
  35. }
  36. catch (Exception ex)
  37. {
  38. MessageBox.Show(ex.Message);
  39. }
  40. }
  41. public void Close()
  42. {
  43. this._odcConnection.Close();
  44. }
  45. public DataTable GetDataTable(string sql)
  46. {
  47. DataTable ds = new DataTable();
  48. OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
  49. try
  50. {
  51. adapter.Fill(ds);
  52. }
  53. catch (Exception ex)
  54. {
  55. throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
  56. }
  57. adapter.Dispose();
  58. return ds;
  59. }
  60. public DataSet GetDataSet(string sql)
  61. {
  62. DataSet ds = new DataSet();
  63. OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
  64. try
  65. {
  66. adapter.Fill(ds);
  67. }
  68. catch (Exception ex)
  69. {
  70. throw new Exception("sql語句: " + sql + " 執行失敗!"+ex.Message);
  71. }
  72. adapter.Dispose();
  73. return ds;
  74. }
  75. /// <summary>
  76. /// 通过条件更新
  77. /// </summary>
  78. /// <param name="TableName"></param>
  79. /// <param name="update"></param>
  80. /// <param name="condition"></param>
  81. public string UpdateByCondition(string TableName, string update, string condition)
  82. {
  83. DataTable result = new DataTable();
  84. string sql = "update " + TableName + " set " + update + " where " + condition;
  85. OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
  86. try
  87. {
  88. adapter.Fill(result);
  89. }
  90. catch (Exception ex)
  91. {
  92. throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
  93. }
  94. adapter.Dispose();
  95. return sql;
  96. }
  97. }
  98. }