.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
在ADO.NET中使用事务保护数据的完整性(4)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

实施事务

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

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();

   }

}

 




相关文章

相关软件