. NET Distributed Transactions example

Source: Internet
Author: User
Tags commit config connect
Distributed using system;//Reference system namespaces
Using system.data.oledb;//references ado.net namespaces
Using system.data;//Reference data space
Using system.runtime.interopservices;//in order to invoke the GUID
Using system.enterpriseservices;//reference COM + name space
Using system.runtime.compilerservices;//compile server at run time
Using system.reflection;//to get a strong Name property with some global properties
Using System.Data.SqlClient;
[Assembly:applicationname ("Comlibrary")]
Strong name file name and file attributes. Generate with Sn.exe, use Sn-k clsstock.snk
[Assembly:assemblykeyfileattribute ("Clsstock.snk")]
[Assembly:assemblykeyname ("Clsstock.snk")]
[Assembly:assemblykeyfileattribute (".. \\.. \\clsStock.snk ")]
[Assembly:assemblykeyname (".. \\.. \\clsStock.snk ")]
Namespace Comlibrary
{
[Transaction (transactionoption.required)]
public class Dboperation:servicedcomponent
{

Private SqlConnection myconnection;
Private SqlCommand mycommand;

<summary>
Connect database
</summary>
<param name= "Connection" >database config infomation
Formatting:datasource= ...; User id= ...; Password= ...
</param>
<returns></returns>
public void Connect ()
{
String connection= "Workstation Id=yanlixin;packet size=4096;user id=sa;data source=\" yanlixin\\scgl\ ";p" +
"Ersist security info=true;initial CATALOG=SCGL;PASSWORD=SCGL";
//
myconnection = new SqlConnection (connection);
Myconnection.open ();

mycommand = new SqlCommand ();
Mycommand.connection = myconnection;

Return
}

<summary>
Execute One INSERT statement and insert one record into the table
</summary>
<param name= "Connection" >database config infomation
Formatting:datasource= ...; User id= ...; Password= ...
</param>
<returns></returns>

public int Commandexcute (string commandstring)
{

int returnvalue = 0;

myCommand.CommandText = CommandString;
returnvalue = Mycommand.executenonquery ();

Return returnvalue;

}

<summary>
Commit the COM + transaction
</summary>

public void Commit ()
{

ContextUtil.SetComplete ();

if (myconnection!=null)
Myconnection.close ();

}

<summary>
Roolback the COM + transaction
</summary>

public void Abort ()
{

ContextUtil.SetAbort ();

if (myconnection!=null)
Myconnection.close ();

}

}
}





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.