473,473 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SqlTransaction and Scope_Identity or another way?

MrMancunian
569 Recognized Expert Contributor
Ok, I'm stuck on a design problem and I need some feedback how to go around it.

CASE:

Pathologists can request stains on certain tissues. It's possible to request more than one stain a time. The program where they make these requests (which is third-party software) creates an XML-file for one stain. So, if two stains are requested on one piece of tissue, it will create two XML-files. The program I'm writing needs to parse those XML-files, merge the stains that are requested on one piece of tissue by one person at the same moment and then insert that as one Request in the database. The database is MS SQL. It consists of 30 tables and isn't very exciting. When the request has been inserted, i use the identifier of that request in another table to create a StainID, which I use to identify the stains in their own table (each stain has it's own table, as they have different properties). After the XML-files have been parsed, merged and inserted into the database, I want to rename the files to .xml.bak. However, before this is done, I need to make sure that the request has been entered into the database correctly.

So, basically, the process should be something like this:

1. Pathologist requests stains on tissue
2. Third-party-software creates XML-files for each stain
3. XML-files are parsed and then merged, based on the request
4. Data is inserted in database
5. XML-files are renamed to *.xml.bak

PROBLEM:

I need to make sure that a request has been inserted in the database and all stains are present. If not, the XML-file has to be parsed again. I was thinking of using a SqlTransaction to make sure that all database events are handled, before the XML-files get renamed. However, there is one big issue. When I insert a new request in the database, I want the identity to be returned, so I can use that in the insert queries for the stains.

Expand|Select|Wrap|Line Numbers
  1. strCommando0 = "INSERT INTO tblAangevraagdMoleculair (AanvraagID, MoleculairID, FixatieID) VALUES (" & intAanvraagID & ", " & intMoleculairID & ", " & intFixatieID & "); SELECT SCOPE_IDENTITY();"
  2. Dim command0 As New SqlClient.SqlCommand(strCommando0, cnnKaSuS)
  3. daMIPAanvraag.InsertCommand = command0
  4. intNieuwBepalingID = daMIPAanvraag.InsertCommand.ExecuteScalar()
  5.  
  6. strCommando = "INSERT INTO tblERBB2PerBepaling (BepalingID, B1_P3, B1_P9, B1_P21, B2_P3, B2_P9, B2_P21, GW_P3, GW_P9, GW_P21) VALUES (" & intNieuwBepalingID & ", '" & strP3B1 & "', '" & strP9B1 & "', '" & strP21B1 & "', '" & strP3B2 & "', '" & strP9B2 & "', '" & strP21B2 & "', '" & strP3GW & "', '" & strP9GW & "', '" & strP21GW & "');"
  7. Dim command6 As New SqlClient.SqlCommand(strCommando, cnnKaSuS)
  8. daMIPERBB2Updaten.InsertCommand = command6
  9. daMIPERBB2Updaten.InsertCommand.ExecuteNonQuery()
As you can see, I need the Scope_Identity for further referencing in other tables. The main problem now is that when I use the SqlTransaction, it won't return any value until the transaction is committed.

Does anyone have an idea how to solve this? I'm not looking for code or a complete solution, but I think it's a design issue.

Thanks,

Steven
Jul 16 '09 #1
3 3108
JosAH
11,448 Recognized Expert MVP
Would it be an option to create (yet) another table where you store the names of the xml files that have successfully completed processing? A trigger could rename the xml file(s) and remove the particular row(s) from that table again.

kind regards,

Jos
Jul 16 '09 #2
MrMancunian
569 Recognized Expert Contributor
Hi Jos,

Thanks for your solution. Storing the names in a separate table will work fine. I also found out that the SqlTransaction does return a Identity, even if the transaction is cancelled later :-)

Steven
Jul 20 '09 #3
capttaz
1 New Member
I would recommend using stored procs to put your transaction in. Use the SQL 2005 try catch block and return the indentity in the try along with the commit. Obviously rollback in the catch. This way you can even make a second attempt to insert a record right in the sp.
Aug 4 '09 #4

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

Similar topics

4
by: Scrappy | last post by:
I have an ASP front end on SQL 2000 database. I have a form that submits to an insert query. The entry field is an "identity" and the primary key. I have used scope_identity() to display the...
1
by: Lauren Quantrell | last post by:
I'm using an Access2K front end on a SQL Server2K backend. I use Scope_Identity() in a lot of stored procedures to pass the newwly inserted record's unique ID to the next select statement in the...
2
by: mahajan.sanjeev | last post by:
Hi, I am having problems with rollback using the SQLTransaction object. I am trying to insert records in two tables in a transaction. I want to rollback all the changes if any exception occurs...
0
by: mahajan.sanjeev | last post by:
Hi All, I am using a SQLTransaction in a .Net application to insert records into a SQL Server table. At one time, there are 5000 or more records to be inserted one by one. It takes some 20-25...
5
by: Larry | last post by:
I am seeing a problem with an ASP application, where I have 2 tables. In the first table, the ASP inserts just 1 row and retrieves the primary key of the new row using SCOPE_IDENTITY. It then uses...
6
by: Hardy Wang | last post by:
Hi all, I have the following codes, but SCOPE_IDENTITY() just returns NULL to me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works fine!! Since I have a trigger on the...
2
by: .Net Newbie | last post by:
Hello, I am somewhat new to .Net and currently working on an intranet site using C# going against SQL Server 2k. I am accepting personal information on a single webform and trying to insert the...
1
by: Justyn | last post by:
Hi everyone, I hope someone can help me out! I have added a trigger that gets fired on INSERT and which itself inserts data into another table WITHOUT an identity I used to get back the...
2
by: needin4mation | last post by:
Hi, I have generated my queries and selected the refresh the table option. This creates a select after the insert to get the scope_identity() so that I can use that key value in another table,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.