c#實(shí)現(xiàn)sqlserver事務(wù)處理示例
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#獲取Description特性的擴(kuò)展類(lèi)詳解
這篇文章主要和大家詳細(xì)介紹一下C#獲取Description特性的擴(kuò)展類(lèi),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,需要的可以參考一下2022-06-06C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類(lèi)型的三種方法
這篇文章主要介紹了C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類(lèi)型的三種方法,本文總結(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èn)題,通過(guò)不同的從網(wǎng)上學(xué)習(xí),最終找到了解決的辦法,下面這篇文章主要給大家介紹了關(guān)于C#連接MySQL數(shù)據(jù)庫(kù)的方法步驟,需要的朋友可以參考下2023-01-01winform獲取當(dāng)前名稱(chēng)實(shí)例匯總
這篇文章主要介紹了winform獲取當(dāng)前名稱(chēng)實(shí)例匯總,包括常見(jiàn)的目錄名、文件名、路徑等,非常實(shí)用,需要的朋友可以參考下2014-10-10C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法
下面小編就為大家?guī)?lái)一篇C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08