Hi, i am trying to insert the contents of a CSV file into a database table using linq2SQL.
I want to be able to rollback the transaction if ANY of the inserts fail but when i try with this code i get the following error at - db.Transaction.Commit()
System.InvalidOperationException was unhandled: This SqlTransaction has completed; it is no longer usable.
Does anyone know what i am doing wrong?
using (DataContext db = new DataContext())
{
db.Connection.Open();
db.Transaction = db.Connection.BeginTransaction();
try
{
foreach (string entry in entries)
{
XXX xxx = new XXX()
{
P1 = "something",
P2 = "something"
};
db.XXXX.InsertOnSubmit(xxx);
db.SubmitChanges();
}
}
catch (Exception)
{
db.Transaction.Rollback();
}
finally
{
db.Connection.Close();
}
db.Transaction.Commit();
}