a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > 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);
                }
            }
        }
    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 羞羞视频网站免费看 | 亚洲第一精品在线 | 成人免费视频网 | 国产免费黄色大片 | 97色资源 | 亚洲视频在线一区二区 | 亚洲一区中文字幕在线观看 | 黄色毛片在线看 | 国产高清美女一级a毛片久久 | 欧美视频在线观看免费 | 日韩视频一区二区三区四区 | 97色资源| 午夜电影网址 | 日本最黄视频 | 一区二区三区欧美 | 黄色在线免费观看视频 | 一级毛片在线看aaaa | 精品九九久久 | 天天拍天天操 | 日韩久久久 | 操片| 国产精品18 | 日韩一区二区三区四区五区六区 | 国产欧美精品一区二区三区 | 亚洲九九九 | 亚洲中国字幕 | 一区不卡| 美日韩三级 | 国产区视频在线 | 成人一边做一边爽爽视频 | 黄色av网站在线免费观看 | 久久久婷婷 | 日本中文字幕一区二区有码在线 | 在线视频亚洲 | 中文字幕亚洲乱码 | 亚洲精品在线免费 | 91免费观看在线 | 日韩中文字幕视频 | 色黄网站| 国产一区精品视频 | 国产日韩欧美一区二区 |