Using starttransaction with C++ builder 6.0 and SDAC

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
boutwater
Posts: 7
Joined: Fri 14 Aug 2009 14:00

Using starttransaction with C++ builder 6.0 and SDAC

Post by boutwater » Thu 05 Apr 2012 23:10

Hello,
I wanted to know the proper way to code using the starttransaction, commit/rollback functionality of the SDAC.

Code: Select all

    case 2: {  // custom Method == 2
      Refresh();
      MSQuery->SQL->Clear();
      MSQuery->SQL->Add(DB->CustomQueryString);
      try {
        MSQuery->Execute();
      } catch (EMSError &E) {
        Answer = false;
        char ErrorMsg[2048];
        UpString(ErrorMsg,E.Message.c_str());
        if (strstr(ErrorMsg,"PRIMARY KEY CONSTRAINT") == NULL) {
          sprintf(ErrorMsg,"Key Convert Error: %s",E.Message.c_str());
          DB->DBErrorNote(ErrorMsg);
          iSET->WriteToDBLog(ErrorMsg);
        }
      }
      if (MSQuery->RecordCount > 0) {
        Data.ErrorCodeID = MSQuery->Fields->Fields[0]->AsString;
        Data.ToDescription = MSQuery->Fields->Fields[1]->AsString;
        Answer = true;
      } else {
        Data.CartonID = "";
      }
      MSQuery->Close();
      break;
    }  // END of Method 2
In the above code, how would I add Starttransaction, commit and rollback?

A secondary question is, will this adversly affect other queries running against this database? The above function could be called from multiple places and multiple applications against the database. Thank you in advance for your help.

I'm using Borland C++ Builder 6.0 pro and the sdac 4.8.

Ben

AndreyZ

Post by AndreyZ » Fri 06 Apr 2012 08:46

Hello,

Here is a code example:

Code: Select all

MSQuery->SQL->Add(DB->CustomQueryString);
MSQuery->Connection->StartTransaction();
try {
  MSQuery->Execute();
  MSQuery->Connection->Commit();
} catch (EMSError &E) {
  MSQuery->Connection->Rollback();
  Answer = false;
...
}
Using transactions affects queries that read or write data to the same tables at the same time. Depending on the transaction isolation level, one query will see or will no see changes that are being made by another query. For more information about transaction isolation levels, please refer to http://msdn.microsoft.com/en-us/library/ms173763.aspx

Post Reply