Hi everyone,
The following wrapper works, but I find it kinda ugly.
If I just change the CommandText of the OleDbCommand, it is simply rewritten
by .NET to the old value. So, I create a new OleDbCommand and copy all
needed information from the generated to the newly created.
Any suggestions to make this more beautifull are welcome
Greetz,
Ben
-----------------------------------
protected OleDbCommand GetUpdateCommand(OleDbCommandBuilder commandBuilder)
{
OleDbCommand updateCommand = commandBuilder.GetUpdateCommand();
// What is the commandtext
string command = updateCommand.CommandText;
// find the first AND in the WHERE clause
// assuming the Primary key is the parameter just befor the first ' AND '
int indexWhere = command.IndexOf(" AND ");
// We only need the first part of the commandstring
string commandTillWhere = command.Substring(0, indexWhere);
// Make a new OledBCommand
OleDbCommand newUpdateCommand = new OleDbCommand( commandTillWhere + " )
" );
// Copy needed properties from the old to the new connection
newUpdateCommand.Connection = updateCommand.Connection;
newUpdateCommand.Transaction = updateCommand.Transaction;
// Loop through all the parameters and make a copy
for (int i = 0 ; i < updateCommand.Parameters.Count ; i++)
{
OleDbParameter par = updateCommand.Parameters;
// For some rare case, we can nut use the method par.get_IsNullable(), so
alsways true
bool isNullable = true;
//Create the new parameter
OleDbParameter newPar = new OleDbParameter( par.ParameterName,
par.OleDbType, par.Size, par.Direction, isNullable, par.Precision,
par.Scale, par.SourceColumn, par.SourceVersion, par.Value );
// Add it to the updateCommand
newUpdateCommand.Parameters.Add(newPar);
}
// return the newly created UpdateCommand
return newUpdateCommand;
}