c#實(shí)現(xiàn)sqlserver事務(wù)處理示例
更新時(shí)間:2014年01月13日 11:46:18 作者:
這篇文章主要介紹了c#實(shí)現(xiàn)sqlserver事務(wù)處理的示例,大家參考使用吧
復(fù)制代碼 代碼如下:
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();
// Attempt to commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
相關(guān)文章
C#ComboBox控件“設(shè)置 DataSource 屬性后無(wú)法修改項(xiàng)集合”的解決方法
這篇文章主要介紹了C#ComboBox控件“設(shè)置 DataSource 屬性后無(wú)法修改項(xiàng)集合”的解決方法 ,需要的朋友可以參考下2019-04-04C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法
這篇文章主要介紹了C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法,本文總結(jié)了Convert.ToDateTime(string)、Convert.ToDateTime(string, IFormatProvider)、DateTime.ParseExact()三種方法,需要的朋友可以參考下2015-07-07DirectoryInfo引用一個(gè)相對(duì)目錄的實(shí)例
這種特殊參數(shù)在Windows的命令提示符或者“運(yùn)行”對(duì)話框中都可以使用,等價(jià)于DOS中的cd命令參數(shù)。直接上代碼,一看你就懂了:2013-04-04C#連接MySQL數(shù)據(jù)庫(kù)的方法步驟
最近兩天在解決C#連接MySql數(shù)據(jù)庫(kù)的問題,通過不同的從網(wǎng)上學(xué)習(xí),最終找到了解決的辦法,下面這篇文章主要給大家介紹了關(guān)于C#連接MySQL數(shù)據(jù)庫(kù)的方法步驟,需要的朋友可以參考下2023-01-01C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法
下面小編就為大家?guī)硪黄狢# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08