ICANN认证国际域名顶级注册机构
网站建设,域名注册,空间租用,软件销售,广告设计 域名注册,空间租用, 网站建设,东莞网页设计,域名注册,网站推广,网站开发,网页设计 网站推广,,百度推广,新浪推广,雅虎推广,网易推广 标志设计,画册设计,包装设计,VI策划,企业形象顾问 连邦软件加盟店,提供销遥行,管家婆,速达,用友ERP,诺顿等软件报价及销售 建站必读 腾达网络,上网帮助中心, 腾达为东莞等地,提供网站建设,网站推广,广告设计,软件销售等优质服务!
建站必读


 
 
当前位置:首页 -> 建站必读 -> .NET技术
在ADO.NET中使用事务保护数据的完整性(4)
实施事务



既然我们已经看了类和成员,让我们来看一下基本的实施情况.接下来的代码是一个简单的情况,使用事务来保证两个存储过程-一个从表中删除库存,另一个增加库存在另个表中,或同时执行,或失败.



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;



…public void SPTransaction(int partID, int numberMoved, int siteID)

{

// Create and open the connection.

SqlConnection conn = new SqlConnection();

string connString = "Server=SqlInstance;Database=Test;"

+ "Integrated Security=SSPI";

conn.ConnectionString = connString;

conn.Open();



// Create the commands and related parameters.

// cmdDebit debits inventory from the WarehouseInventory

// table by calling the DebitWarehouseInventory

// stored procedure.

SqlCommand cmdDebit =

new SqlCommand("DebitWarehouseInventory", conn);

cmdDebit.CommandType = CommandType.StoredProcedure;

cmdDebit.Parameters.Add("@PartID", SqlDbType.Int, 0, "PartID");

cmdDebit.Parameters["@PartID"].Direction =

ParameterDirection.Input;

cmdDebit.Parameters.Add("@Debit", SqlDbType.Int, 0, "Quantity");

cmdDebit.Parameters["@Debit"].Direction =

ParameterDirection.Input;



// cmdCredit adds inventory to the SiteInventory

// table by calling the CreditSiteInventory

// stored procedure.

SqlCommand cmdCredit =

new SqlCommand("CreditSiteInventory", conn);

cmdCredit.CommandType = CommandType.StoredProcedure;

cmdCredit.Parameters.Add("@PartID", SqlDbType.Int, 0, "PartID");

cmdCredit.Parameters["@PartID"].Direction =

ParameterDirection.Input;

cmdCredit.Parameters.Add

("@Credit", SqlDbType.Int, 0, "Quantity");

cmdCredit.Parameters["@Credit"].Direction =

ParameterDirection.Input;

cmdCredit.Parameters.Add("@SiteID", SqlDbType.Int, 0, "SiteID");

cmdCredit.Parameters["@SiteID"].Direction =

ParameterDirection.Input;



// Begin the transaction and enlist the commands.

SqlTransaction tran = conn.BeginTransaction();

cmdDebit.Transaction = tran;

cmdCredit.Transaction = tran;



try

{

// Execute the commands.

cmdDebit.Parameters["@PartID"].Value = partID;

cmdDebit.Parameters["@Debit"].Value = numberMoved;

cmdDebit.ExecuteNonQuery();



cmdCredit.Parameters["@PartID"].Value = partID;

cmdCredit.Parameters["@Credit"].Value = numberMoved;

cmdCredit.Parameters["@SiteID"].Value = siteID;

cmdCredit.ExecuteNonQuery();



// Commit the transaction.

tran.Commit();

}

catch(SqlException ex)

{

// Roll back the transaction.

tran.Rollback();



// Additional error handling if needed.

}

finally

{

// Close the connection.

conn.Close();

}

}









// Commit the outer transaction.



tran.Commit();



}



catch(OleDbException ex)



{



//Roll back the transaction.



tran.Rollback();







//Additional error handling if needed.



}



finally



{



// Close the connection.



conn.Close();



}



}






 
 
本站关键词:
|东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司||东莞主机申请|东莞空间申请|东莞网站建设|东莞网页设计|东莞网络公司|网站建设|网页设计|网络公司|网站制作|网站开发|东莞域名空间|东莞广告公司|
域名注册 | 虚拟主机 | 网站建设 | 网站推广 | 广告设计 | 帮助中心 | 软件中心 | 关于腾达
东莞市腾达信息工程有限公司      本站网络实名: 东莞网站建设
地址:东莞市新城市中心区第一国际B座610   邮编:523007
电话:0769-22026071 22026072 传真:0769-22026076 24小时服务热线:13712032014
© 2002~2005 腾达网络   版权所有