国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#實現sqlserver事務處理示例

2020-01-24 02:55:06
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临朐县| 长汀县| 浪卡子县| 天气| 英吉沙县| 宣恩县| 卓资县| 伊吾县| 陈巴尔虎旗| 凭祥市| 灵石县| 遂平县| 绵阳市| 榆中县| 泽州县| 雅安市| 宁晋县| 肇源县| 天全县| 额尔古纳市| 天长市| 揭东县| 肇东市| 天全县| 临夏市| 泰顺县| 平武县| 得荣县| 怀集县| 石嘴山市| SHOW| 区。| 新河县| 荣昌县| 晋宁县| 绥化市| 襄城县| 城固县| 娱乐| 呼伦贝尔市| 宁德市|