简体   繁体   中英

ODBC transaction does not roll back

I have referred this to perform rollback operation in my wpf c# application. The code that I tried is as follows:

using (OdbcConnection connection = new OdbcConnection("connectionString"))
        {
            OdbcCommand command = new OdbcCommand();
            OdbcTransaction transaction = null;
            command.Connection = connection;
            try
            {
                connection.Open();
                transaction = connection.BeginTransaction();

                command.Connection = connection;
                command.Transaction = transaction;

                command.CommandText = "INSERT INTO TableA (A, B, C) VALUES (10,10,10)";
                command.ExecuteNonQuery();
                command.CommandText = "NSERT INTO TableB (D,E,F) VALUES (20,20,20)";
                command.ExecuteNonQuery();
                transaction.Commit();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                try
                {
                    transaction.Rollback();
                }
                catch
                {

                }
            }

Intentionally the second query has been made wrong. My intention is that when I enter the catch block on calling transaction.Rollback() the values added due to executing of the first query in TableA are not reflected since Rollback was called. However this is not the case the values are not rolledback and are present in TableA. I have searched various resources online with no luck. I cannot use SqlConnection instead of OdbcConnection my application does not support that. Is there any work around this or alternative method that can achieve what I have in mind. Please help me out.

You have basically MSDN example. I had once another problem with ODBC and the issue was with ODBC vendor drivers. I would strongly recommend check that possibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM