Muiltiple Stored Procedure Within Transaction

L

Leon

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.
 
R

Rob

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.

Fairly straight-forward:

// 1) Create your database connection
IDbConnection dbConn = new
(System.Data.SqlClient.SqlConnection("connection string");

// 2) Create a transaction object
IDbTransaction dbTrans = myConn .BeginTransaction();

// 3) Execute your stored procedures, passing transaction object as
parameter
IDbCommand cmd = dbConn .CreateCommand();
cmd.CommandText = "stored procedure name";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = dbTrans;

// 4) Add any parameters here
IDataParameter parameter = cmd.CreateParameter();
parameter.ParameterName = CreateCollectionParameterName("param name");
parameter.DbType = DbType.(whatever);
parameter.Value = (whatever);
cmd.Parameters.Add(parameter);

// 5) Execute command
cmd.ExecuteNonQuery(); // or cmd.ExecuteReader or cmd.ExecuteScalar

// 6) Repeat 3), 4) & 5) for next procedure

// 7) Commit the transaction if sucessful otherwise rollback
if (success)
dbTrans.Commit()
else
dbTrans.Rollback()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top