人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

在ADO.NET中運用事務(wù)保護數(shù)據(jù)的完整性(4)

在ADO.NET中運用事務(wù)保護數(shù)據(jù)的完整性(4)

更新時間:2022-09-19 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

實施事務(wù)

既然我們已經(jīng)看了類和成員,讓我們來看一下基本的實施情況.接下來的代碼是一個簡單的情況,使用事務(wù)來保證兩個存儲過程-一個從表中刪除庫存,另一個增加庫存在另個表中,或同時執(zhí)行,或失敗.

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

}

}


溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 肇州县| 铁岭县| 监利县| 东阿县| 江永县| 延吉市| 喜德县| 无极县| 横山县| 乌恰县| 普格县| 黔江区| 乌兰浩特市| 滁州市| 石景山区| 敦化市| 平原县| 西畴县| 夹江县| 抚远县| 北海市| 印江| 兴海县| 慈溪市| 怀集县| 天峨县| 河北省| 青州市| 平陆县| 江达县| 宜城市| 浮山县| 凤山县| 东乌珠穆沁旗| 察哈| 辽中县| 安平县| 望都县| 合川市| 新巴尔虎右旗| 福清市|