【发布时间】:2014-01-27 01:54:54
【问题描述】:
我已使用此代码从我的 Access 表中创建了一个 DataTable。
DataTable dataTable = new DataTable();
...
using (OleDbConnection oledbConnection = new OleDbConnection(connection))
{
oledbConnection.Open();
using (OleDbCommand oledbCommand = new OleDbCommand("SELECT * FROM Student", oledbConnection))
{
using (OleDbDataAdapter oledbDataAdapter = new OleDbDataAdapter(oledbCommand))
{
oledbDataAdapter.Fill(dataTable);
}
}
}
我后来通过使用此代码插入新行来更改数据表。
DataRow dataRow = dataTable.NewRow();
dataRow["FirstName"] = "John";
dataRow["LastName"] = "Connor";
dataTable.Rows.Add(dataRow);
dataTable.GetChanges();
我怎样才能在我的访问数据库中带来这种变化? 下面的代码是否正确?
using (OleDbConnection oledbConnection = new OleDbConnection(connectionString))
{
OleDbDataAdapter oledbDataAdapter = new OleDbDataAdapter();
oledbDataAdapter.SelectCommand = new OleDbCommand(queryString, oledbConnection);
OleDbCommandBuilder oledbCommandBuilder = new OleDbCommandBuilder(oledbDataAdapter);
connection.Open();
oledbDataAdapter.DeleteCommand = oledbCommandBuilder.GetDeleteCommand(true);
oledbDataAdapter.InsertCommand = oledbCommandBuilder.GetInsertCommand(true);
oledbDataAdapter.UpdateCommand = oledbCommandBuilder.GetUpdateCommand(true);
oledbDataAdapter.Update(dataTable);
connection.Close();
}
【问题讨论】:
标签: c# database datatable oledb