OleDbTransaction.Commit メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > OleDbTransaction.Commit メソッドの意味・解説 

OleDbTransaction.Commit メソッド

データベース トランザクションコミットます。

名前空間: System.Data.OleDb
アセンブリ: System.Data (system.data.dll 内)
構文構文

例外例外
使用例使用例

OleDbConnection と OleDbTransaction を作成する例を次に示します。BeginTransaction、CommitRollback の各メソッド使い方示します

Public Sub ExecuteTransaction(ByVal
 connectionString As String)

    Using connection As New OleDbConnection(connectionString)
        Dim command As New
 OleDbCommand()
        Dim transaction As OleDbTransaction

        ' Set the Connection to the new OleDbConnection.
        command.Connection = connection

        ' Open the connection and execute the transaction.
        Try
            connection.Open()

            ' Start a local transaction.
            transaction = connection.BeginTransaction()

            ' Assign transaction object for a pending local transaction.
            command.Connection = connection
            command.Transaction = transaction

            ' Execute the commands.
            command.CommandText = _
                "Insert into Region (RegionID, RegionDescription) VALUES (100,
 'Description')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "Insert into Region (RegionID, RegionDescription) VALUES (101,
 'Description')"
            command.ExecuteNonQuery()

            ' Commit the transaction.
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            ' Try to rollback the transaction
            Try
                    transaction.Rollback()

            Catch
                ' Do nothing here; transaction is not active.
            End Try
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using
End Sub
public void ExecuteTransaction(string
 connectionString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand();
        OleDbTransaction transaction = null;

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the transaction.
        try
        {
            connection.Open();

            // Start a local transaction
            transaction = connection.BeginTransaction();

            // Assign transaction object for a pending local transaction.
            command.Connection = connection;
            command.Transaction = transaction;

            // Execute the commands.
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100,
 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101,
 'Description')";
            command.ExecuteNonQuery();

            // Commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            try
            {
                // Attempt to roll back the transaction.
                transaction.Rollback();
            }
            catch
            {
                // Do nothing here; transaction is not active.
            }
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
using System;
using System.Data;
using System.Data.OleDb;

class Class1
{
    static void Main()
    {
    }

    public void ExecuteTransaction(string
 connectionString)
    {
        using (OleDbConnection connection =
                   new OleDbConnection(connectionString))
        {
            OleDbCommand command = new OleDbCommand();
            OleDbTransaction transaction = null;

            // Set the Connection to the new OleDbConnection.
            command.Connection = connection;

            // Open the connection and execute the transaction.
            try
            {
                connection.Open();

                // Start a local transaction
                transaction = connection.BeginTransaction();

                // Assign transaction object for a pending local transaction.
                command.Connection = connection;
                command.Transaction = transaction;

                // Execute the commands.
                command.CommandText =
                    "Insert into Region (RegionID, RegionDescription) VALUES (100,
 'Description')";
                command.ExecuteNonQuery();
                command.CommandText =
                    "Insert into Region (RegionID, RegionDescription) VALUES (101,
 'Description')";
                command.ExecuteNonQuery();

                // Commit the transaction.
                transaction.Commit();
                Console.WriteLine("Both records are written to database.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                try
                {
                    // Attempt to roll back the transaction.
                    transaction.Rollback();
                }
                catch
                {
                    // Do nothing here; transaction is not active.
                }
            }
            // The connection is automatically closed when the
            // code exits the using block.
        }
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

OleDbTransaction.Commit メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



OleDbTransaction.Commit メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS