Browse Source

ACCESS数据库连接方法调整

Hcsy 7 years ago
parent
commit
f97ef53b8f
1 changed files with 55 additions and 6 deletions
  1. 55 6
      UAS-MES/DataOperate/AccessDBHelper.cs

+ 55 - 6
UAS-MES/DataOperate/AccessDBHelper.cs

@@ -4,6 +4,7 @@ using System.Data;
 using System.Data.OleDb;
 using System.Linq;
 using System.Text;
+using System.Windows;
 
 namespace UAS_MES.DataOperate
 {
@@ -11,7 +12,7 @@ namespace UAS_MES.DataOperate
     {
         private string _fileName;
         private string _connectionString;
-        private OleDbConnection _odcConnection;
+        private OleDbConnection _odcConnection = null;
 
         public AccessDBHelper(string fileName)
         {
@@ -19,6 +20,13 @@ namespace UAS_MES.DataOperate
             this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";
         }
 
+
+        public AccessDBHelper(string fileName,string password)
+        {
+            this._fileName = fileName;
+            this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Jet OLEDB:Database Password="+password+";";
+        }
+
         public void Open()
         {
             try
@@ -28,9 +36,9 @@ namespace UAS_MES.DataOperate
                 // 打开连接
                 this._odcConnection.Open();
             }
-            catch (Exception)
+            catch (Exception ex)
             {
-                throw new Exception("嘗試打开 " + this._fileName + " 失敗, 請確認文件是否存在!");
+                MessageBox.Show(ex.Message);
             }
         }
 
@@ -39,19 +47,60 @@ namespace UAS_MES.DataOperate
             this._odcConnection.Close();
         }
 
+
+        public DataTable GetDataTable(string sql)
+        {
+            DataTable ds = new DataTable();
+            OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
+            try
+            {
+                adapter.Fill(ds);
+            }
+            catch (Exception ex)
+            {
+                throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
+            }
+            adapter.Dispose();
+            return ds;
+        }
+
         public DataSet GetDataSet(string sql)
         {
             DataSet ds = new DataSet();
+            OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
             try
             {
-                OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
                 adapter.Fill(ds);
             }
-            catch (Exception)
+            catch (Exception ex)
             {
-                throw new Exception("sql語句: " + sql + " 執行失敗!");
+                throw new Exception("sql語句: " + sql + " 執行失敗!"+ex.Message);
             }
+            adapter.Dispose();
             return ds;
         }
+
+        /// <summary>
+        /// 通过条件更新
+        /// </summary>
+        /// <param name="TableName"></param>
+        /// <param name="update"></param>
+        /// <param name="condition"></param>
+        public string UpdateByCondition(string TableName, string update, string condition)
+        {
+            DataTable result = new DataTable();
+            string sql = "update " + TableName + " set " + update + " where " + condition;
+            OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
+            try
+            {
+                adapter.Fill(result);
+            }
+            catch (Exception ex)
+            {
+                throw new Exception("sql語句: " + sql + " 執行失敗!" + ex.Message);
+            }
+            adapter.Dispose();
+            return sql;
+        }      
     }
 }