保存包含多个表的数据集

时间:2014-11-14 21:48:58

标签: c# visual-studio ms-access

对于我正在处理的项目,我正在使用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; 

        }



    }

1 个答案:

答案 0 :(得分:0)

您错过的是SQLDataReader

public SQLDataReader someReader
{
 get {return this.Reader("students");}
}