保存包含多个表的数据集
本文关键字:数据集 包含多 保存 | 更新日期: 2023-09-27 18:30:31
对于我正在从事的项目,我正在使用从服务器下载的 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;
}
}
你错过的是SQLDataReader
public SQLDataReader someReader
{
get {return this.Reader("students");}
}