G
Guest
I am trying to update a large set of data (VS2005 & SQL2000). I have a
temp_table setup to hold my data taht i read in from my datafile. I BCP all
of the data to that table and I'm then running insert/update statement(s) on
it to move the corresponding data to my production server. However, I can
not garauntee that all of the data I am receiving is up to date so sometimes
there are exceptions thrown when a FK doesn't exist (as it should) however
I'd like for my data processing to continue and just discard the row that is
causing the problem. As far as I know there is no way for me to validate
that all of the data I have is correct, just rely upon my RI to keep it as
correct as possible. Is it possible for my Program to continue after an
exception is thrown and caught? I have attached a portion of the exceptions,
and some of the code as well.
INSERT statement conflicted with COLUMN FOREIGN KEY constraint
'FK_debt_client'. The conflict occurred in database 'Depted-TEST', table
'client', column 'clientid'.\r\nUPDATE statement conflicted with COLUMN
FOREIGN KEY constraint 'FK_debt_client'. The conflict occurred in database
'Depted-TEST', table 'client', column 'clientid'
static void BcpUpdate(SqlConnection DWConnection, DataTable datatable,
String UpdateString, String InsertString)
{
SqlTransaction tx = DWConnection.BeginTransaction();
try {
using(SqlBulkCopy bcp = new SqlBulkCopy(DWConnection,
SqlBulkCopyOptions.Default, tx))
{
bcp.DestinationTableName = "temp_" + datatable;
bcp.BatchSize = 5000;
bcp.WriteToServer(datatable);
}
tx.Commit();
tx = DWConnection.BeginTransaction();
SqlCommand cmd = new SqlCommand(null, DWConnection, tx);
cmd.CommandText = @"" + InsertString + UpdateString;
cmd.CommandTimeout = 0;
cmd.ExecuteNonQuery();
tx.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
//tx.Rollback(); taken out for troubleshooting
}
}
temp_table setup to hold my data taht i read in from my datafile. I BCP all
of the data to that table and I'm then running insert/update statement(s) on
it to move the corresponding data to my production server. However, I can
not garauntee that all of the data I am receiving is up to date so sometimes
there are exceptions thrown when a FK doesn't exist (as it should) however
I'd like for my data processing to continue and just discard the row that is
causing the problem. As far as I know there is no way for me to validate
that all of the data I have is correct, just rely upon my RI to keep it as
correct as possible. Is it possible for my Program to continue after an
exception is thrown and caught? I have attached a portion of the exceptions,
and some of the code as well.
INSERT statement conflicted with COLUMN FOREIGN KEY constraint
'FK_debt_client'. The conflict occurred in database 'Depted-TEST', table
'client', column 'clientid'.\r\nUPDATE statement conflicted with COLUMN
FOREIGN KEY constraint 'FK_debt_client'. The conflict occurred in database
'Depted-TEST', table 'client', column 'clientid'
static void BcpUpdate(SqlConnection DWConnection, DataTable datatable,
String UpdateString, String InsertString)
{
SqlTransaction tx = DWConnection.BeginTransaction();
try {
using(SqlBulkCopy bcp = new SqlBulkCopy(DWConnection,
SqlBulkCopyOptions.Default, tx))
{
bcp.DestinationTableName = "temp_" + datatable;
bcp.BatchSize = 5000;
bcp.WriteToServer(datatable);
}
tx.Commit();
tx = DWConnection.BeginTransaction();
SqlCommand cmd = new SqlCommand(null, DWConnection, tx);
cmd.CommandText = @"" + InsertString + UpdateString;
cmd.CommandTimeout = 0;
cmd.ExecuteNonQuery();
tx.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
//tx.Rollback(); taken out for troubleshooting
}
}