Groups | Blog | Home
all groups > dotnet web services enhancements > november 2004 >

dotnet web services enhancements : Database Transactions


Merline Martina
11/22/2004 10:49:02 PM
Can anyone tell me how to work with database transactions in a web service.
Can anyone please give me a sample code.
--
Cool!!
John Paul. A
11/22/2004 10:59:01 PM
Dear Merline Martina,
Here is a sample code which explains about the database transactions in a
web service.

[Web Method]
public string adduser( string UserName, string UserPassword)
{
string uname,upassword;
uname=UserNmae;
upassword=userPassword;
string mycon=”Intial Catalog=test;Data Source= johnpaul\DBTEST; Integrated
security =SSPI”;

//declare a sql connection by building a object named myconnection
SqlConnection mysqlConnection =new SqlConnection(mycon);
// declare sql command by using a object named mycommand
SqlCommand mysqlCommand = new SqlCommand();
// using sql transaction class
SqlTransaction myTrans;

// Open the connection.
mysqlConnection.Open();
// Assign the connection property.
mysqlCommand.Connection = mysqlConnection;

// Begin the transaction.
myTrans = myConnection.BeginTransaction();

// Assign transaction object for a pending local transaction
mysqlCommand.Transaction = myTrans;

try
{
// Insert the user record.
mysqlCommand.CommandText = "Insert into useraccount VALUES
('"+uname+"','"+ufamily+"')";
mysqlCommand.ExecuteNonQuery();
// pass the data .transaction complete
myTrans.Commit();
return "transaction completed";
}
catch(Exception e)
{
// transaction cancel
myTrans.Rollback();
return e.ToString()+"transaction abort";
}

}

Rgds,
John Paul. A

[quoted text, click to view]
Merline Martina
11/23/2004 3:21:02 AM
Thanks...
Can you give me few links please...

[quoted text, click to view]
John Paul. A
11/23/2004 3:53:04 AM
Have a look at this page.

Web Services Transaction (WS-Transaction)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnglobspec/html/ws-transaction.asp

Rgds,
John Paul. A

[quoted text, click to view]
AddThis Social Bookmark Button