0

I have done simple My sql transactions. But Is there a way to make transactions through c# ?

Example:

Insert all rows from a file or never insert anything.

Are there any examples or directions for doing this in c#? I have only done single query execution through parametrized c# syntax. writing a delete query I think is an overkill and does not suit my needs.

Community
  • 1
  • 1
Dexters
  • 2,419
  • 6
  • 37
  • 57
  • http://stackoverflow.com/questions/815586/entity-framework-using-transactions-or-savechangesfalse-and-acceptallchanges – King Friday Feb 22 '13 at 17:54

1 Answers1

3

Certainly.

If you are using Entity Framework with MySQL, SaveChanges() will be performed within a transaction. It will either all succeed, or all will fail together (if required you can have finer control over that aspect using TransactionScope).

Using ADO.Net, you can use a TransactionScope as outlined here:

https://stackoverflow.com/a/3321030/141172

TransactionScope also works with DataTable

http://www.codingbeaver.com/myblog/2010/02/19/update-datatable-in-transaction/

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Hi Thanks a lot. Looks really promising. Also as I searched I came across Data Tables. Does it provide the same functionality? And Can you comment on TransactionScope's cost on DB? Does it does like 'I will Insert all your rows. Let me see.. oops! An exception. Now I have to rollback everything :(" - I guess this is how it happens internally. or "Hey! I can rollback smartly" - I dont have a clue on how this smart can be though :) – Dexters Feb 22 '13 at 21:07
  • 1
    You can use TransactionScope with DataTables (updated post with link). There is a cost to using transactions. Unless you have thousands of rows, it will be hard to measure in the grand scheme of most applications. The cost of *not* using transactions can be severe (corrupted data). As a rule of thumb, if I have many rows to insert and for some reason cannot use a more optimized solution such as SqlBulkCopy, I'll insert with transactions in blocks of 1000 (assuming the application can tolerate the first 1000 being inserted but not the next 1000). – Eric J. Feb 22 '13 at 21:36