473,425 Members | 1,917 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,425 software developers and data experts.

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.BeginTransaction
Try
call (New SqlCommand("INSERT INTO TransactionDemo " + "(Text)
VALUES ('Row1');", db, transaction)).ExecuteNonQuery
call (New SqlCommand("INSERT INTO TransactionDemo " + "(Text)
VALUES ('Row2');", db, transaction)).ExecuteNonQuery
call (New SqlCommand("INSERT INTO CrashMeNow VALUES " + "('Die',
'Die', 'Die');", db, transaction)).ExecuteNonQuery
transaction.Commit
Catch sqlError As SqlException
transaction.Rollback
End Try
db.Close
End Sub
+++>>>> But how can i do this with a stored procedure each time ?

Call (New SqlCommand("mySP", myConnexion,
myTransaction)).ExecuteNonQuery()

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

how can i do ?

Nov 23 '05 #1
9 10896
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.BeginTransaction
Try
call (New SqlCommand("INSERT INTO TransactionDemo " + "(Text)
VALUES ('Row1');", db, transaction)).ExecuteNonQuery
call (New SqlCommand("INSERT INTO TransactionDemo " + "(Text)
VALUES ('Row2');", db, transaction)).ExecuteNonQuery
call (New SqlCommand("INSERT INTO CrashMeNow VALUES " + "('Die',
'Die', 'Die');", db, transaction)).ExecuteNonQuery
transaction.Commit
Catch sqlError As SqlException
transaction.Rollback
End Try
db.Close
End Sub
+++>>>> But how can i do this with a stored procedure each time ?

Call (New SqlCommand("mySP", myConnexion,
myTransaction)).ExecuteNonQuery()

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.BeginTransaction
cmd.transaction = transaction
cmd.commandtype = storedprocedure
cmd.parameter.add(...)
Try
cmd.parameter(x).value = ....
cmd.executenonquery
'note that you don't have to make a new command object every time.
cmd.parameter(x).value = ....
cmd.executenonquery

Catch sqlError As SqlException
transaction.Rollback
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.
"ucasesoftware" <uc***********@hotmail.fr> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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.

"ucasesoftware" <uc***********@hotmail.fr> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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.executenonquery

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

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

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.
"ucasesoftware" <uc***********@hotmail.fr> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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
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...
7
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...
3
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...
11
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...
3
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...
1
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...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
2
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...
7
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...
0
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.