Here is a function that can do the job---you must pass the corresponding
command to this method. If the datatable has some inserted record and you
don't supply insertCommand, it will generate an error. I use this method to
update a dataTable(with changes only DataTable.GetChanges) in the database
in a batch. You have to build your own command and use stored procedure to
do the update, insert or delete.
public int ExecuteNonQuery(string connectionString,OleDbCommand
updateCommand,OleDbCommand insertCommand,OleDbCommand
deleteCommand,DataTable dataTable,bool useTransaction)
{
int retval=0;
OleDbTransaction txn=null;
OleDbConnection cn=null;
//Check if the DataTable has updated records. If so, an update command is
required.
DataTable updatedTable=dataTable.GetChanges(DataRowState.Modified);
if ((updatedTable !=null) && (updateCommand==null))
{
throw new Exception("Update Failed. Data has been modified but no update
command is specified.");
}
//Check if the DataTable has inserted records. If so, an insert command
is required.
DataTable insertedTable=dataTable.GetChanges(DataRowState.Added);
if ((insertedTable !=null) && (insertCommand==null))
{
throw new Exception("Update Failed. Data has been inserted but no insert
command is specified.");
}
//Check if the DataTable has deleted records. If so, a delete command is
required.
DataTable deletedTable=dataTable.GetChanges(DataRowState.Deleted);
if ((deletedTable !=null) && (deleteCommand==null))
{
throw new Exception("Update Failed. Data has been deleted but no delete
command is specified.");
}
try
{
//create & open a OleDbConnection, and dispose of it after we are done.
cn = new OleDbConnection(connectionString);
cn.Open();
//Create a data adapter
OleDbDataAdapter da = new OleDbDataAdapter();
//Assign commands to the adapter
if (updatedTable !=null)
{
updateCommand.Connection=cn;
da.UpdateCommand=updateCommand;
}
if (insertedTable !=null)
{
insertCommand.Connection=cn;
da.InsertCommand=insertCommand;
}
if (deletedTable !=null)
{
deleteCommand.Connection=cn;
da.DeleteCommand=deleteCommand;
}
//Create a new transaction if useTransaction is true
if (useTransaction==true)
{
txn = cn.BeginTransaction();
//Set the Transaction property of the DataAdapter's Commands.
if (updatedTable !=null)
{
da.UpdateCommand.Transaction = txn;
}
if (insertedTable !=null)
{
da.InsertCommand.Transaction = txn;
}
if (deletedTable !=null)
{
da.DeleteCommand.Transaction = txn;
}
}
//Submit the changes.
retval=da.Update(dataTable);
//Commit the changes and close the connection.
if (txn !=null)
{
txn.Commit();
}
return retval;
}
catch(Exception ex)
{
try
{
if (txn !=null)
{
txn.Rollback();
}
}
catch (OleDbException OleDbex)
{
throw OleDbex;
}
throw ex;
}
finally
{
if (cn !=null)
{
if (cn.State ==ConnectionState.Open)
{
cn.Close();
}
}
}
}