要使用OleDbDataAdapter更新DataTable,可以按照以下步骤进行操作:
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myDatabase.accdb;");
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand("SELECT * FROM myTable", connection);
adapter.InsertCommand = new OleDbCommand("INSERT INTO myTable (column1, column2) VALUES (?, ?)", connection);
adapter.UpdateCommand = new OleDbCommand("UPDATE myTable SET column1 = ?, column2 = ? WHERE id = ?", connection);
adapter.DeleteCommand = new OleDbCommand("DELETE FROM myTable WHERE id = ?", connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
// 修改数据
dataTable.Rows[0]["column1"] = "new value";
// 添加数据
DataRow newRow = dataTable.NewRow();
newRow["column1"] = "value1";
newRow["column2"] = "value2";
dataTable.Rows.Add(newRow);
// 删除数据
dataTable.Rows[1].Delete();
adapter.Update(dataTable);
以上就是使用OleDbDataAdapter更新DataTable的基本步骤。请根据实际情况调整代码,例如更改SQL语句和表名、列名等。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 在C#中使用静态变量处理资源共享问题