Hi
try the following example to use Transaction with Linq :
– First Example :-
DataClasses1DataContext db = new DataClasses1DataContext();
// Get all customers list
IQueryable<Customer> list = from x in db.Customers
where x.Code > 0
select x;
Customer cust1 = list.Single<Customer>(x => x.Code == 1);
Customer cust2 = list.Single<Customer>(x => x.Code == 2);
// Update the first customer object
cust1.FirstName = “yasser”;
cust1.LastName = “zaid”;
// Delete the second customer object
db.Customers.Remove(cust2);
// The update & delete operation will execte
// in the same transaction
db.Connection.Open();
db.Transaction = db.Connection.BeginTransaction();
try
{
db.SubmitChanges();
db.Transaction.Commit();
}
catch
{
db.Transaction.Rollback();
// And do some error handling…
}
finally
{
db.Connection.Close();
db.Transaction = null;
}
– Second Example :-
DataClasses1DataContext db = new DataClasses1DataContext();
// Get all customers list
IQueryable<Customer> list = from x in db.Customers
select x;
Customer cust1 = list.Single<Customer>(x => x.Code == 1);
Customer cust2 = list.Single<Customer>(x => x.Code == 2);
// Update the first customer object
cust1.FirstName = “yasser”;
cust1.LastName = “zaid”;
// Delete the second customer object
db.Customers.Remove(cust2);
using (TransactionScope ts = new TransactionScope())
{
// The update & delete operation will execte in
//the same transaction
db.SubmitChanges();
ts.Complete();
}
Hope this helps
Good Luck