1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transactionscope?view=netframework-4.0
try
{
using (TransactionScope scope = new TransactionScope())
{
// Do Operation 1
// Do Operation 2
//...
// if all the coperations complete successfully, this would be called and commit the trabsaction.
// In case of an exception, it wont be called and transaction is rolled back
scope.Complete();
}
}
catch (ThreadAbortException ex)
{
// Handle exception
}
You should use TransactionScope because of the flexibility it provides. It will make it easier in the future to include things other than ADO.NET calls into a transaction.
SqlTransaction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqltransaction?view=netframework-4.0
using (SqlTransaction tran = conn.BeginTransaction())
{
try
{
// Insert into File Registry
using (SqlCommand cmd = new SqlCommand(fileQuery, conn, tran))
{
String[] parts = article.DocumentFileLink.Split('/');
String file = parts[parts.Length - 1];
String filename = "News/" + file;
cmd.Parameters.AddWithValue("@UploadedFileName", file);
cmd.Parameters.AddWithValue("@FileName", filename);
cmd.Parameters.AddWithValue("@FileUrl", article.DocumentFileLink);
Int64 id = (Int64)cmd.ExecuteScalar();
SqlCommand cmd2 = new SqlCommand(artFileQuery, conn, tran);
cmd2.Parameters.AddWithValue("@ArticleID", article.ID);
cmd2.Parameters.AddWithValue("@FileRegistryID", id);
cmd2.ExecuteScalar();
}
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
// throw;
}
finally
{
TaskBucket.completedTasks.Add(article.ID);
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
origin - https://www.pipiscrew.com/?p=14718 transactionscope