473,626 Members | 3,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transaction with stored procedure

i need to use this :

Private Shared Sub Demo1()
Dim db As SqlConnection = New SqlConnection(" connstringhere" )
Dim transaction As SqlTransaction
db.Open
transaction = db.BeginTransac tion
Try
call (New SqlCommand("INS ERT INTO TransactionDemo " + "(Text)
VALUES ('Row1');", db, transaction)).E xecuteNonQuery
call (New SqlCommand("INS ERT INTO TransactionDemo " + "(Text)
VALUES ('Row2');", db, transaction)).E xecuteNonQuery
call (New SqlCommand("INS ERT INTO CrashMeNow VALUES " + "('Die',
'Die', 'Die');", db, transaction)).E xecuteNonQuery
transaction.Com mit
Catch sqlError As SqlException
transaction.Rol lback
End Try
db.Close
End Sub
+++>>>> But how can i do this with a stored procedure each time ?

Call (New SqlCommand("myS P", myConnexion,
myTransaction)) .ExecuteNonQuer y()

but i need the command type : stored procedure and the parameters ????

how can i do ?

Nov 23 '05 #1
9 10939
ucasesoftware wrote:
i need to use this :

Private Shared Sub Demo1()
Dim db As SqlConnection = New SqlConnection(" connstringhere" )
Dim transaction As SqlTransaction
db.Open
transaction = db.BeginTransac tion
Try
call (New SqlCommand("INS ERT INTO TransactionDemo " + "(Text)
VALUES ('Row1');", db, transaction)).E xecuteNonQuery
call (New SqlCommand("INS ERT INTO TransactionDemo " + "(Text)
VALUES ('Row2');", db, transaction)).E xecuteNonQuery
call (New SqlCommand("INS ERT INTO CrashMeNow VALUES " + "('Die',
'Die', 'Die');", db, transaction)).E xecuteNonQuery
transaction.Com mit
Catch sqlError As SqlException
transaction.Rol lback
End Try
db.Close
End Sub
+++>>>> But how can i do this with a stored procedure each time ?

Call (New SqlCommand("myS P", myConnexion,
myTransaction)) .ExecuteNonQuer y()

but i need the command type : stored procedure and the parameters ????

how can i do ?


To call a stored proc do this: (note: I just typed this in here, there
are errors but it should give you the idea)

Dim db As SqlConnection = New SqlConnection(" connstringhere" )
Dim transaction As SqlTransaction
dim cmd as new sqlcommand
db.Open
transaction = db.BeginTransac tion
cmd.transaction = transaction
cmd.commandtype = storedprocedure
cmd.parameter.a dd(...)
Try
cmd.parameter(x ).value = ....
cmd.executenonq uery
'note that you don't have to make a new command object every time.
cmd.parameter(x ).value = ....
cmd.executenonq uery

Catch sqlError As SqlException
transaction.Rol lback
End Try
db.Close
End Sub
Nov 23 '05 #2
but i have to do 3 stored procedure in 1 transaction...

it's possible with this exemple ?

Nov 23 '05 #3
You can do all the SQL statements in a single stored procedure, and handle
the transaction within the stored procedure instead of in the .net code.
Just pass in all of the necessary parameters to process all 3 statements.
"ucasesoftw are" <uc***********@ hotmail.fr> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
but i have to do 3 stored procedure in 1 transaction...

it's possible with this exemple ?

Nov 23 '05 #4
negative

cause one of the 3 are in another database

Nov 23 '05 #5
I see...

You could try using linked servers/remote servers to do the updates from one
stored proc, but that can cause more problems.

Here is one approach I used when trying to update two databases, one oracle
and one SQL Server...

I started a transaction (oracle stored procedure) against the one database.
If the transaction succeeded, I executed the (SQL) stored procedure against
the second database. If the second (SQL) transaction succeeded, I commited
the first (oracle) transaction. If it failed I rolled back the transaction.

not sure if this is at all helpful. Unfortunately I no longer have the code
that I used for this either.

"ucasesoftw are" <uc***********@ hotmail.fr> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
negative

cause one of the 3 are in another database

Nov 23 '05 #6
If the 3rnd don't succes how you rolled back the 1rst one ?

Nov 23 '05 #7
ucasesoftware wrote:
but i have to do 3 stored procedure in 1 transaction...

it's possible with this exemple ?


Yes, as long as you use the same transaction object, they are all
included in the same transaction. You can even use the same
commandojbect. Just change the commandtext property.

Something like

try

Dim Cmd as new SqlCommand
cmd.connection = connection
cmd.transaction = transaction
cmd.commandtype = storedproecdure

cmd.parameters. add(..)
cmd.parameters. add(..)
cmd.parameters. value = ...
cmd.parameters. value = ...
cmd.commandtext = "Proc1"
cmd.executenonq uery

cmd.parameters. clear
cmd.parameters. add(..)
cmd.parameters. add(..)
cmd.parameters. value = ...
cmd.parameters. value = ...
cmd.commandtext = "Proc2"
cmd.executenonq uery

cmd.parameters. clear
cmd.parameters. add(..)
cmd.parameters. add(..)
cmd.parameters. value = ...
cmd.parameters. value = ...
cmd.commandtext = "Proc3"
cmd.executenonq uery

catch ex as exception
'rollback
end try
Nov 23 '05 #8
Thee are only 2 transactions in my app...

The first is done as a transaction in classic asp, and is not committed
until the second succeeds.

If the second hits any errors then the SP for the second transaction
performs the rollback and returns an error to the application.

When the app gets the error it rolls back the first transaction. If it gets
a success it commits the first transaction.
"ucasesoftw are" <uc***********@ hotmail.fr> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
If the 3rnd don't succes how you rolled back the 1rst one ?

Nov 23 '05 #9
thx a lot Chris

it's help me a lot :)

Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2370
by: Deepak Mehta | last post by:
i have to update two tables from ASP pages with same data but i want that both of them should be updated at one time. If either of them is not updated then my transaction should roll back.I want this thing to be in a stored procedure. so that i have to write an execute statement only on the ASP page and pass the parameters. Looking forward for ur reply DEEPAK
7
9203
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
3
8716
by: Mark | last post by:
If a java applicaiton using the type 4 driver calls a DB2 stored procedure, does the stored procedure need to do its own commit when updates are completed? If the stored procedure does a commit or rollback, does that affect the UOW for any SQL that was directly issued by the java program before calling the stored procedure?
11
5224
by: harborboy76 | last post by:
Hi, I have a stored procedure that does a lot of INSERT/UDATE to 3 tables. And When I call the stored procedure, I get a Transaction Log Full error. When I want to do is turning off the transaction log on those 3 tables that the stored procedure is using. Now, since I call the stored procedure on the command line (CLI), where do I run ALTER TABLE ... ACTIVATE NOT LOGGED INITIALLY statement ? Do I have to put them inside the stored...
3
4043
by: Irfan | last post by:
There are several ways of handling Transactions in DotNet. Some of them are 1. Using COM+ Serviced Component. 2. Using ADO .Net 3. using stored procedure What is the best way of handling Transaction in DotNet Enterprise Application interms of performance, maintainance and scalability? How feasible it would be if we go for stored procedure option, considering
1
1504
by: Belee | last post by:
I am developing a c# program VS 2003 and I have created my own stored procedure to insert data into 3 tables The following is the stored procedure, the code and the error message from visual studio ide The following the stored procedure CREATE PROCEDURE dbo.NewControlAndNormalAccoun @AccountNo numeric(18) @AccountName nvarchar(50) @AccountType char(16)
2
5449
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
2
1997
by: Leon | last post by:
How can I code a stored procedure "within my codebehind page" that execute two or more stored procedures within a transaction? if so how would I pass values to each parameter? i.e. Begin Tran exec sp1 exec sp2 Commit Tran .....of course some error checking.
7
9702
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason for this being to stop the user thinking the application has frozen when in fact it is just waiting for a long SP to complete. Another reason for doing it like this is that I also have had a problem in the past where the SP takes longer than the...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5571
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.