Uploaded image for project: 'CUBRID APIs'
  1. CUBRID APIs
  2. APIS-184

[ODBC]how to do auto_commit off in C#

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: ODBC-Aprium
    • Component/s: ODBC
    • Labels:
      None

      Description

      USER ISSUE = http://www.cubrid.com/zbxe/334127

      [ODBC] hasn't how to do auto_commit off in C#

        Activity

        Hide
        cn15800 谢韦华[Bert] added a comment -

        When test program throw an exception,Transaction will rollback,and the valu 1 do not insert into table

        public static void ExecuteTransaction(string connectionString)
        {
        using (OdbcConnection connection =
        new OdbcConnection(connectionString))
        {
        OdbcCommand command = new OdbcCommand();
        OdbcTransaction transaction = null;

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

        // Open the connection and execute the transaction.
        try

        { connection.Open(); ExecuteSQL("drop table if exists t;", connection); ExecuteSQL("create table t(dt int);", connection); // 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 t (dt) VALUES (1)"; command.ExecuteNonQuery(); System.Console.WriteLine("Insert 1"); //make a Exception here,then chect the table t //there is not value 1 command.CommandText = "Insert into t (dt) VALUES (2)"; command.ExecuteNonQuery(); System.Console.WriteLine("Insert 2"); // 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. Console.WriteLine("before end."); output(); transaction.Rollback(); Console.WriteLine("Rollback end."); output(); }

        catch

        { // Do nothing here; transaction is not active. }

        }
        finally

        { connection.Close(); Console.ReadKey(); }

        }
        }

        Show
        cn15800 谢韦华[Bert] added a comment - When test program throw an exception,Transaction will rollback,and the valu 1 do not insert into table public static void ExecuteTransaction(string connectionString) { using (OdbcConnection connection = new OdbcConnection(connectionString)) { OdbcCommand command = new OdbcCommand(); OdbcTransaction transaction = null; // Set the Connection to the new OdbcConnection. command.Connection = connection; // Open the connection and execute the transaction. try { connection.Open(); ExecuteSQL("drop table if exists t;", connection); ExecuteSQL("create table t(dt int);", connection); // 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 t (dt) VALUES (1)"; command.ExecuteNonQuery(); System.Console.WriteLine("Insert 1"); //make a Exception here,then chect the table t //there is not value 1 command.CommandText = "Insert into t (dt) VALUES (2)"; command.ExecuteNonQuery(); System.Console.WriteLine("Insert 2"); // 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. Console.WriteLine("before end."); output(); transaction.Rollback(); Console.WriteLine("Rollback end."); output(); } catch { // Do nothing here; transaction is not active. } } finally { connection.Close(); Console.ReadKey(); } } }

          People

          • Assignee:
            cn15800 谢韦华[Bert]
            Reporter:
            jeongbin.lee Jeongbin Lee
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: