473,322 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C# Transactions - Multiple Queries

Hi there

I would like to write code that stops any user to view or alter data on multiple tables until I am completed. I am only keeping the DB (SQL Server) busy for a short while. Below is the code that I have implemented. (Only part of the code) I am not sure if this is sufficient. Help will be appreciated! I am using the Same Transaction on different Commands on different tables.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Transaction = SystellenceData.Connection.BeginTransaction(IsolationLevel.Serializable);
  3.  
  4. SqlCommand Command = new SqlCommand("SELECT ref_no_counter FROM docs_os_combination WHERE os_comb_id = " + OSCombID.ToString(), SystellenceData.Connection, Transaction);
  5.  
  6. object ObjectResult = Command.ExecuteScalar();
  7. if (ObjectResult != DBNull.Value)
  8. {
  9. Result = (int)(ObjectResult);
  10. string NewDocumentRefNo = RefNo.Trim().Replace("$$$$", Result.ToString().PadLeft(4, '0'));
  11.  
  12. Command.CommandText = "SELECT COUNT(*) AS NoRecords FROM docs_document WHERE (document_ref_no = '" + NewDocumentRefNo + "')";
  13.  
  14. if (((int)Command.ExecuteScalar()) == 0)
  15. {
  16. int NewCountValue = Result + 1;
  17. if (NewCountValue > 9999)
  18.  NewCountValue = 0;
  19.  
  20. Command.CommandText = "UPDATE docs_os_combination SET ref_no_counter = " + NewCountValue.ToString() + " WHERE os_comb_id = " + OSCombID.ToString();
  21.  
  22. if (Command.ExecuteNonQuery() != 1)
  23. {
  24. Transaction.Rollback();
  25. Result = -1;
  26. RefNo = "";
  27. }
  28.  else
  29. {
  30. Transaction.Commit();
  31. RefNo = NewDocumentRefNo;
  32. }
  33.  
Aug 12 '10 #1
1 4051
Christian Binder
218 Expert 100+
I think, it works that way. Another example implementation of Transactions can be found on msdn.
I'd recommend using SqlParameters instead of concating strings to avoid SQL-injection.
Aug 12 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Geoff Berrow | last post by:
This is puzzling. If I run $sql = "INSERT INTO customer (custid,username,password,title,firstname,lastname,street,towncity,postcode,telno,mobile,email,glasses) VALUES...
0
by: Urko | last post by:
Hello, I want to return multiple queries from a procedure in Oracle, I know that o can do that with REF CURSOR. But I have a problem I have a query that has a while loop and in each iteration a...
0
by: Clint Pachl | last post by:
When executing multiple queries, which return a result set, must mysql_free_result be called after retrieving each result set using mysql_store_result? My concern is that if the result was not...
2
by: caro | last post by:
Hi I am trying to write two Select * statements to the same text file using bcp (from a stored procedure). But cannot find a way of appending to a file using bcp. Does anyone know if this...
3
by: Gord | last post by:
I would like to create a summary report from the results of 11 queries (based on 2 tables). All the queries have the same format and return 3 numbers (Count, Current Year Bal, Last Year Bal.)...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
1
by: mattcatmattcat | last post by:
I have a VB7 aspx file I am creating that requires multiple queries each dependant on the previous queries results. If I run these queries in foxpro, I just run a query then create a cursor with...
8
by: beretta819 | last post by:
Ok, so I apologize in advance for the wordiness of what follows... (I am not looking for someone to make this for me, but to point me in the right direction for the steps I need to take.) I was...
7
by: vaiism | last post by:
I am creating a report that outputs the contact information and details about a water treatment plant, and needs to include information about people who work there. If I tie all the information...
4
by: Akhenaten | last post by:
I am currently using enterprise manager to run multiple queries on a single table in a DB. I refresh these queries every few minutes. Due to the huge number of them I was looking for a better way...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.