Everything Web

Using CommandBuilder in a transaction (Copy data - part 2).

June 24, 2009dgrinberg

Once I got the initial data transfer up and running, I needed to place the whole thing into a transaction, I was doing multiple table transfers (with some data manipulation).  I could not have half a database transferred, even if I did throw an error.

I simply threw in an SqlTransaction, and made everything run inside that.

However, I hit an exception when using the SqlCommandBuilder to create the insert command:

ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized.

I did not realize why the CommandBuilder needed a transaction, nor could I add the transaction to the builder.  After all, it was only creating a SQL statement wasn't it??

I don't know if I was slow on the uptake...  (I am writing this blog to hopefully help others out there who might be just as slow as I) but it took me forever to realise that it needed to run the SELECT SQL statement to get the details for creating the INSERT. 

As I had created the SqlCommand by placing the SELECT statement into the constructor, I had not thought about assigning the transaction to the SelectCommand.

  1.  
  2. DataTable configResults #008000;">= #008000;">new DataTable#008000;">(#666666;">'Config'#008000;">)#008000;">;  
  3. SqlConnection sourceConnection #008000;">= #008000;">new SqlConnection#008000;">(_sourceConnection#008000;">)#008000;">;
  4. sourceConnection#008000;">.#0000FF;">Open#008000;">(#008000;">)#008000;">;  
  5.  
  6. SqlDataAdapter a #008000;">= #008000;">new SqlDataAdapter#008000;">(#666666;">'SELECT * FROM Config', sourceConnection#008000;">)#008000;">;
  7. a#008000;">.#0000FF;">AcceptChangesDuringFill #008000;">= #0600FF; font-weight: bold;">false#008000;">;  
  8. a#008000;">.#0000FF;">Fill#008000;">(configResults#008000;">)#008000;">;  
  9. a#008000;">.#0000FF;">Dispose#008000;">(#008000;">)#008000;">;  
  10.  
  11. SqlConnection destConnection #008000;">= #008000;">new SqlConnection#008000;">(_destinationConnection#008000;">)#008000;">;  
  12. destConnection#008000;">.#0000FF;">Open#008000;">(#008000;">)#008000;">;  
  13.  
  14. SqlTransaction transaction #008000;">= destConnection#008000;">.#0000FF;">BeginTransaction#008000;">(#008000;">)#008000;">;  
  15. #0600FF; font-weight: bold;">try  
  16. #008000;">{      
  17.     SqlDataAdapter profileAdapter #008000;">= #008000;">new SqlDataAdapter#008000;">(#666666;">'SELECT * FROM Config', destConnection#008000;">)#008000;">;
  18.     profileAdapter#008000;">.#0000FF;">SelectCommand#008000;">.#0000FF;">Transaction #008000;">= transaction#008000;">;
  19.  
  20.     SqlCommandBuilder builder #008000;">= #008000;">new SqlCommandBuilder#008000;">(profileAdapter#008000;">)#008000;">;      
  21.  
  22.     profileAdapter#008000;">.#0000FF;">InsertCommand #008000;">= builder#008000;">.#0000FF;">GetInsertCommand#008000;">(#008000;">)#008000;">;
  23.     profileAdapter#008000;">.#0000FF;">InsertCommand#008000;">.#0000FF;">Transaction #008000;">= transaction#008000;">;
  24.     profileAdapter#008000;">.#0000FF;">Update#008000;">(configResults#008000;">)#008000;">;
  25.  
  26.     profileAdapter#008000;">.#0000FF;">Dispose#008000;">(#008000;">)#008000;">;
  27.     builder#008000;">.#0000FF;">Dispose#008000;">(#008000;">)#008000;">;
  28.  
  29.     transaction#008000;">.#0000FF;">Commit#008000;">(#008000;">)#008000;">;  
  30. #008000;">}  
  31. #0600FF; font-weight: bold;">catch  
  32. #008000;">{
  33.     transaction#008000;">.#0000FF;">Rollback#008000;">(#008000;">)#008000;">;  
  34. #008000;">}  
  35. #0600FF; font-weight: bold;">finally  
  36. #008000;">{      
  37.     transaction#008000;">.#0000FF;">Dispose#008000;">(#008000;">)#008000;">;  
  38. #008000;">}  

So you can see from the code snippet above, in line 18, I had to assign the transaction to the SELECT command after creating the DataAdapter.

Trackback URL for this post:

http://ontheperiphery.veraida.com/trackback/34

Alana Whelan

Major thankies for the article post.Much thanks again. Will read on...

Ella Gamache

Thanks so much for the post.Really thank you!

Elizabeth Hubbert

I think this is a real great blog article.Really looking forward to read more. Fantastic.

Chase Phelps

I value the blog article.Really looking forward to read more. Much obliged.

Lydia Garrard

Really appreciate you sharing this blog.Really looking forward to read more. Awesome.