I've used this code to create me a DataTable
from my Access table.
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);
}
}
}
I later changed the datatable by inserting a new row with this code.
DataRow dataRow = dataTable.NewRow();
dataRow["FirstName"] = "John";
dataRow["LastName"] = "Connor";
dataTable.Rows.Add(dataRow);
dataTable.GetChanges();
How can I bring this change in my access database? Is correct the following code?
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();
}
You'll have to reopen the connection and do an insert into that database. The SQL could look something like this:
string sql = String.Format("INSERT INTO Students(FirstName,LastName) VALUES({0},{1})", "John", "Connor")