Process Multiple SQL statements at a time using transactions

Source: Internet
Author: User

/// <Summary>
/// Execute multiple SQL statements to implement database transactions.
/// </Summary> sql2000 database
/// <Param name = "SQLStringList"> Multiple SQL statements </param>
Public static void ExecuteSqlTran (List <string> SQLStringList)
{
Using (SqlConnection conn = new SqlConnection (SqlHelper. ConString ))
{
Conn. Open ();
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = conn;
SqlTransaction tx = conn. BeginTransaction ();
Cmd. Transaction = tx;
Try
{
For (int n = 0; n <SQLStringList. Count; n ++)
{
String strsql = SQLStringList [n]. ToString ();
If (strsql. Trim (). Length> 1)
{
Cmd. CommandText = strsql;
Cmd. ExecuteNonQuery ();
}
}
Tx. Commit ();
}
Catch (System. Data. SqlClient. SqlException E)
{
Tx. Rollback ();
Throw new Exception (E. Message );
}
}
}

Foreground call:

Try
{
// 1. Check the member balance
Decimal usermoney = SqlDal. EntityUserinfo. GetModel (int. Parse (cookieUserID). Money;
// 2. Modify the balance
Decimal Zmoney = usermoney + moAD. Commission;
// Write an SQL statement
List <string> SQLStringList = new List <string> ();
String clickSql = "insert into [user] (name, age) values ('" + nickname + "', '" + 4 years old + "')";
String userSql = "update [class] set [name] = '" + kindergarten + "'where id =" + 2 + "";
SQLStringList. Add (clickSql );
SQLStringList. Add (userSql );
SqlDal. SqlHelper. ExecuteSqlTran (SQLStringList );

// Database operation successful
// Prompt
CommonClass. Xmls xmls1 = new CommonClass. Xmls ();
String path1 = CommonClass. Unit. GetMapPath (@ "/Admin/Configs/SysSettingInfo. config ");
String ClickTishi = xmls1.GetXmlNode (path1, "SysSettingInfo/ClickTishi ");
// Replace the character
ClickTishi = ClickTishi. Replace ("[$]", moAD. Commission. ToString ("0.00 "));
Context. Response. Write (ClickTishi); // output
}
Catch (Exception ex)
{
// Response. Write (ex. Message );
Context. Response. Write ("operation failed! "+ Ex. Message); // output
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.