C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to use SqlTransaction object in C#


Posted Date: 24 Apr 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Saleth Prakash       Member Level: Bronze
Rating:     Points: 10



Introduction



Well as a improvement, this is my first article in csharp corner. Here i have just given a simple demonstration on the usage of SqlTransaction class in C#


Here i have just used a insert query to insert two columns in a table. So for i have used stored procedures.

The Connection String for the database connection is given in the Web.config File as:


<connectionStrings>
<add name="myConnectionString" connectionString="data source=.; initial catalog=myDB; user id=sa;
password=sa"/>
</connectionStrings>


Next in the button click event:

string name = txtName.Text;
string email = txtEmail.Text;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlTransaction myTransaction = null;
try
{
myConnection.Open();
myTransaction = myConnection.BeginTransaction();
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Connection = myConnection;
myCommand.CommandText = "maillist_insert";
myCommand.Transaction = myTransaction;
SqlParameter myParameter1 = new SqlParameter("@uname", name);
SqlParameter myParameter2 = new SqlParameter("@email", email);
myCommand.Parameters.Add(myParameter1);
myCommand.Parameters.Add(myParameter2);
int rowsAffected = myCommand.ExecuteNonQuery();
myTransaction.Commit();
lblMsg.Text = "Transaction Committed";
}
catch (SqlException ex)
{
myTransaction.Rollback();
lblMsg.Text = "Transaction Rollbacked due to " + ex.Message;
}
finally
{
myConnection.Close();
}






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
SqlTransaction  .  Example  .  Dot net framework  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Organize your "using" statements
Previous Resource: What is Application Domain?
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween masks

Contact Us    Privacy Policy    Terms Of Use