2014-11-14 30 views
0

對於我正在處理的項目,我正在使用下載的服務器的Access數據庫來存儲數據。一旦文件被下載,我打開數據庫並將其複製到數據集中,以便更容易地編輯數據。保存包含多個表的數據集

我現在的問題是,我需要將數據集保存回訪問數據庫,但在程序執行期間,我還向數據集添加了新列,那麼是否有方法可以更新訪問數據庫在與新數據和列一起下載後存儲在E:\驅動器中,或者我必須從頭開始創建新的數據庫。

代碼我用來加載和複製數據集

private void accessConnect() 
    { 
     //Assign values to access database variables 
     Connection = new OleDbConnection(); 
     command = new OleDbCommand(); 
     adapter = new OleDbDataAdapter(); 
     databaseDataSet = new DataSet(); 

     //Assign location of database to connection variable 
     connection.ConnectionString = 
      @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\EAttendance.accdb;" + 
      "Persist Security Info=False"; 

     //Establish connection with database 
     command.Connection = connection; 

     //Copy all tables into a c# dataset 
     try 
     { 
      //Select the user table in the database 
      command.CommandText = "SELECT * FROM users"; 
      adapter.SelectCommand = command; 

      //Copy table into dataset 
      adapter.Fill(databaseDataSet,"users"); 

      //Select the students table in the database 
      command.CommandText = "SELECT * FROM students"; 
      adapter.SelectCommand = command; 

      //copy the students database into the dataset 
      adapter.Fill(databaseDataSet, "students"); 

     } 

     //catch exception and display error if application fails to read database 
     catch (OleDbException) 
     { 
      //Display error in form title bar 
      this.Text = "Error #102 : Database Read Error"; 

      // Set connection value to false 
      connectionBoolean = false; 

     } 



    } 
+0

如果要添加新的列到DataSet的表,那麼你需要重新對這些列訪問文件。保存數據集不會在Access文件上創建新列。有些命令允許更改現有的模式。你可以在這裏找到[引用](http://msdn.microsoft.com/en-us/library/dn123881.aspx) – Steve 2014-11-14 21:55:30

回答

0

你錯過了什麼是SqlDataReader的

public SQLDataReader someReader 
{ 
get {return this.Reader("students");} 
} 
相關問題