472,780 Members | 1,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 10751
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...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.