Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2023-01

Porting Delphi (2007/XE5) application, how to handle ADODB?

9 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I have a number of "older" applications developed on Windows using Delphi7-2007-XE5, which use ADODB for database accesses. The database is Microsoft SQLSERVER.

Now I no longer have the old Borland software installed (issues with Windows 10 etc) so I want to port the projects to FPC/Lazarus. I have ported some other non-database applications which (with some effort) seems to have worked out OK.

Now I am facing the challenge of doing it on an adodb database enabled application and I would like to know if there is some "compatible" way of doing it, which is described somewhere?

Bo Berglund Developer in Sweden

Reply by email

Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 12 lines
I have a number of "older" applications developed on Windows using Delphi7-2007-XE5, which use ADODB for database accesses. The database is Microsoft SQLSERVER. Now I no longer have the old Borland software installed (issues with Windows 10 etc) so I want to port the projects to FPC/Lazarus. I have ported some other non-database applications which (with some effort) seems to have worked out OK. Now I am facing the challenge of doing it on an adodb database enabled application and I would like to know if there is some "compatible" way of doing it, which is described somewhere?

Yes.

You need to use SQLDB, and the TMSSQLConnection.

See https://wiki.freepascal.org/SQLdb_Tutorial1 for a simple explanation.

The page https://wiki.freepascal.org/SQLdb_Package also contains some explanations for setup.

There are some other pages in the wiki.

A more in-depth explanation is in the lazarus book.

Or you can look at

https://www.freepascal.org/~michael/articles/

several articles discuss SQLDB, in particular:

https://www.freepascal.org/~michael/articles/lazviddb/lazviddb.pdf

That should be enough to get you started.

Michael.

Reply by email

Bo Berglund
to Michael Van Canneyt
Michael Van Canneyt wrote
That should be enough to get you started. Michael.

Thanks!
I will go there and check what I can do.
I believe that I have isolated the DB stuff fairly well so hopefully only a few procedures need be touched.
I have NOT dropped components onto a Dataform for example....

(I think that was the name used in Delphi where you could drop invisible components to get them into the RAD system. Long time ago now, though...)

Bo Berglund Developer in Sweden

Reply by email

Bo Berglund
to Michael Van Canneyt: Yes. You need to use SQLDB, and the TMSSQLConnection. See…
Michael Van Canneyt wrote 41 lines
On Mon, 23 Jan 2023, Bo Berglund via lazarus wrote: > I have a number of "older" applications developed on Windows using > Delphi7-2007-XE5, which use ADODB for database accesses. > The database is Microsoft SQLSERVER. > > Now I no longer have the old Borland software installed (issues with Windows 10 > etc) so I want to port the projects to FPC/Lazarus. > I have ported some other non-database applications which (with some effort) > seems to have worked out OK. > > Now I am facing the challenge of doing it on an adodb database enabled > application and I would like to know if there is some "compatible" way of doing > it, which is described somewhere? Yes. You need to use SQLDB, and the TMSSQLConnection. See https://wiki.freepascal.org/SQLdb_Tutorial1 for a simple explanation. The page https://wiki.freepascal.org/SQLdb_Package also contains some explanations for setup. There are some other pages in the wiki. A more in-depth explanation is in the lazarus book. Or you can look at https://www.freepascal.org/~michael/articles/ several articles discuss SQLDB, in particular: https://www.freepascal.org/~michael/articles/lazviddb/lazviddb.pdf That should be enough to get you started. Michael.

Thanks!

Almost there but not quite...
I am using Lazarus 2.2.4 with fpc 3.2.2 on a Windows 10 box.

The application seems to have been converted pretty well using the Lazarus Delphi Conversion function to migrate from Delphi with some extra defines needing adjustments. But these have been minor considering the size of the application.

But the ADODB conversion is now halted on the TSqlQuery and TMSSQLConnection items which operate so differently from ADODB...

I added this in uses:

mssqlconn, //Had to add this in order to use TMSSQLConnection sqldb,

This made the code pass the defines of the objects:

  TAppDbHandler = class
  private
    FConn: TMSSQLConnection;  // TADOConnection;
    FQuery: TSQLQuery;        // TADOQuery;
    FDatabaseServer,
    FDatabase,
    FConnString,
    FDbUser,
    FDbPassword,
    FUserName,
    FUserPassword: string;

But next I get a lot of errors because of the differences between this and the Delphi ADODB we used before.

I got lost here...

constructor TAppDbHandler.Create;
begin
  FConn := TMSSQLConnection.Create(NIL);   //was: TADOConnection.Create(NIL);
  FQuery := TSQLQuery.Create(NIL); //was: TADOQuery.Create(NIL);
  FQuery.Connection := FConn; //<== Triggers an error
  FDatabaseServer :=
ReadRegString(HKEY_CURRENT_USER,'Software\...\AppManager','DatabaseServer','servername');
  FDatabase :=
ReadRegString(HKEY_CURRENT_USER,'Software\...\AppManager','Database','APPMANAGER');
  FConnString := 'Provider=SQLOLEDB.1;Persist Security Info=False;' +
                 'Network Library=DBMSSOCN;Data Source=' + FDatabaseServer + ';'
+
                 'Initial Catalog=' + FDatabase;
  FDbUser := '****';
  FDbPassword := '****';
end;

Is there some example which does not use component drag-drop and properties settings in the GUI but does all of it in code towards MSSQLserver?

That would probably help in setting up the database functions properly.

Bo Berglund Developer in Sweden

Reply by email

Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 88 lines
On Mon, 23 Jan 2023 09:59:14 +0100 (CET), Michael Van Canneyt via lazarus wrote: > > > On Mon, 23 Jan 2023, Bo Berglund via lazarus wrote: > >> I have a number of "older" applications developed on Windows using >> Delphi7-2007-XE5, which use ADODB for database accesses. >> The database is Microsoft SQLSERVER. >> >> Now I no longer have the old Borland software installed (issues with Windows 10 >> etc) so I want to port the projects to FPC/Lazarus. >> I have ported some other non-database applications which (with some effort) >> seems to have worked out OK. >> >> Now I am facing the challenge of doing it on an adodb database enabled >> application and I would like to know if there is some "compatible" way of doing >> it, which is described somewhere? > > Yes. > > You need to use SQLDB, and the TMSSQLConnection. > > See https://wiki.freepascal.org/SQLdb_Tutorial1 for a simple explanation. > > The page https://wiki.freepascal.org/SQLdb_Package also contains some explanations > for setup. > > There are some other pages in the wiki. > > A more in-depth explanation is in the lazarus book. > > Or you can look at > > https://www.freepascal.org/~michael/articles/ > > several articles discuss SQLDB, in particular: > > https://www.freepascal.org/~michael/articles/lazviddb/lazviddb.pdf > > That should be enough to get you started. > > Michael. Thanks! Almost there but not quite... I am using Lazarus 2.2.4 with fpc 3.2.2 on a Windows 10 box. The application seems to have been converted pretty well using the Lazarus Delphi Conversion function to migrate from Delphi with some extra defines needing adjustments. But these have been minor considering the size of the application. But the ADODB conversion is now halted on the TSqlQuery and TMSSQLConnection items which operate so differently from ADODB... I added this in uses: mssqlconn, //Had to add this in order to use TMSSQLConnection sqldb, This made the code pass the defines of the objects: TAppDbHandler = class private FConn: TMSSQLConnection; // TADOConnection; FQuery: TSQLQuery; // TADOQuery; FDatabaseServer, FDatabase, FConnString, FDbUser, FDbPassword, FUserName, FUserPassword: string; But next I get a lot of errors because of the differences between this and the Delphi ADODB we used before. I got lost here... constructor TAppDbHandler.Create; begin FConn := TMSSQLConnection.Create(NIL); //was: TADOConnection.Create(NIL); FQuery := TSQLQuery.Create(NIL); //was: TADOQuery.Create(NIL); FQuery.Connection := FConn; //<== Triggers an error
FQuery.Database:=FConn;
Show quoted text 15 lines
FDatabaseServer := ReadRegString(HKEY_CURRENT_USER,'Software\...\AppManager','DatabaseServer','servername'); FDatabase := ReadRegString(HKEY_CURRENT_USER,'Software\...\AppManager','Database','APPMANAGER'); FConnString := 'Provider=SQLOLEDB.1;Persist Security Info=False;' + 'Network Library=DBMSSOCN;Data Source=' + FDatabaseServer + ';' + 'Initial Catalog=' + FDatabase; FDbUser := '****'; FDbPassword := '****'; end; Is there some example which does not use component drag-drop and properties settings in the GUI but does all of it in code towards MSSQLserver?

Try this:

FConn.DatabaseName:=FDatabase;
FConn.HostName:=FDatabaseServer;
FConn.Params.Add('Provider=SQLOLEDB.1');
FConn.Params.Add('Persist Security Info=False');
FConn.Params.Add('Network Library=DBMSSOCN');
FConn.UserName:=FDbUser;
FConn.PasswWord:=FDBPassword;

it is all pretty generic. I am not sure that the Params are needed, they look more like some ODBC/ADO specific things. The MS-SQL connection of Free Pascal does not use ODBC/ADO. So I would start without the params, only add them if it does not work without them.

One thing: make sure your SQL server accepts TCP/IP connections. You must do this in the Microsoft Management Console. Google is your friend there.

Michael.

Reply by email

Bo Berglund
to Michael Van Canneyt
Michael Van Canneyt wrote 129 lines
On Fri, 10 Feb 2023, Bo Berglund via lazarus wrote: > On Mon, 23 Jan 2023 09:59:14 +0100 (CET), Michael Van Canneyt via lazarus > wrote: > >> >> >> On Mon, 23 Jan 2023, Bo Berglund via lazarus wrote: >> >>> I have a number of "older" applications developed on Windows using >>> Delphi7-2007-XE5, which use ADODB for database accesses. >>> The database is Microsoft SQLSERVER. >>> >>> Now I no longer have the old Borland software installed (issues with Windows 10 >>> etc) so I want to port the projects to FPC/Lazarus. >>> I have ported some other non-database applications which (with some effort) >>> seems to have worked out OK. >>> >>> Now I am facing the challenge of doing it on an adodb database enabled >>> application and I would like to know if there is some "compatible" way of doing >>> it, which is described somewhere? >> >> Yes. >> >> You need to use SQLDB, and the TMSSQLConnection. >> >> See https://wiki.freepascal.org/SQLdb_Tutorial1 for a simple explanation. >> >> The page https://wiki.freepascal.org/SQLdb_Package also contains some explanations >> for setup. >> >> There are some other pages in the wiki. >> >> A more in-depth explanation is in the lazarus book. >> >> Or you can look at >> >> https://www.freepascal.org/~michael/articles/ >> >> several articles discuss SQLDB, in particular: >> >> https://www.freepascal.org/~michael/articles/lazviddb/lazviddb.pdf >> >> That should be enough to get you started. >> >> Michael. > > Thanks! > > Almost there but not quite... > I am using Lazarus 2.2.4 with fpc 3.2.2 on a Windows 10 box. > > The application seems to have been converted pretty well using the Lazarus > Delphi Conversion function to migrate from Delphi with some extra defines > needing adjustments. But these have been minor considering the size of the > application. > > But the ADODB conversion is now halted on the TSqlQuery and TMSSQLConnection > items which operate so differently from ADODB... > > I added this in uses: > > mssqlconn, //Had to add this in order to use TMSSQLConnection > sqldb, > > This made the code pass the defines of the objects: > > TAppDbHandler = class > private > FConn: TMSSQLConnection; // TADOConnection; > FQuery: TSQLQuery; // TADOQuery; > FDatabaseServer, > FDatabase, > FConnString, > FDbUser, > FDbPassword, > FUserName, > FUserPassword: string; > > > But next I get a lot of errors because of the differences between this and the > Delphi ADODB we used before. > > I got lost here... > > constructor TAppDbHandler.Create; > begin > FConn := TMSSQLConnection.Create(NIL); //was: TADOConnection.Create(NIL); > FQuery := TSQLQuery.Create(NIL); //was: TADOQuery.Create(NIL); > FQuery.Connection := FConn; //<== Triggers an error FQuery.Database:=FConn; > FDatabaseServer := > ReadRegString(HKEY_CURRENT_USER,'Software\...\AppManager','DatabaseServer','servername'); > FDatabase := > ReadRegString(HKEY_CURRENT_USER,'Software\...\AppManager','Database','APPMANAGER'); > FConnString := 'Provider=SQLOLEDB.1;Persist Security Info=False;' + > 'Network Library=DBMSSOCN;Data Source=' + FDatabaseServer + ';' > + > 'Initial Catalog=' + FDatabase; > FDbUser := '****'; > FDbPassword := '****'; > end; > > > Is there some example which does not use component drag-drop and properties > settings in the GUI but does all of it in code towards MSSQLserver? Try this: FConn.DatabaseName:=FDatabase; FConn.HostName:=FDatabaseServer; FConn.Params.Add('Provider=SQLOLEDB.1'); FConn.Params.Add('Persist Security Info=False'); FConn.Params.Add('Network Library=DBMSSOCN'); FConn.UserName:=FDbUser; FConn.PasswWord:=FDBPassword; it is all pretty generic. I am not sure that the Params are needed, they look more like some ODBC/ADO specific things. The MS-SQL connection of Free Pascal does not use ODBC/ADO. So I would start without the params, only add them if it does not work without them. One thing: make sure your SQL server accepts TCP/IP connections. You must do this in the Microsoft Management Console. Google is your friend there.

I found this tutorial:
https://www.streetinfo.lu/computing/lazarus/project/use_mssql.html

It is still using RAD style drag-drop of the database components...

But from it I could deduce what seems to be what is needed for my conversion. I have set up the constructor such:

constructor TAppDbHandler.Create;
begin
  FConn  := TMSSQLConnection.Create(NIL);   //was TADOConnection.Create(NIL);
  FTrans := TSQLTransaction.Create(NIL);
  FQuery := TSQLQuery.Create(NIL); // was TADOQuery.Create(NIL);
  FConn.Transaction := FTrans;
  FTrans.DataBase := FConn;
  FQuery.Transaction := FTrans;
  FDatabaseServer :=
ReadRegString(HKEY_CURRENT_USER,'Software\Mycompany\AppManager','DatabaseServer','servername');
  FConn.HostName := FDatabaseServer;
  FDatabase :=
ReadRegString(HKEY_CURRENT_USER,'Software\Mycompany\AppManager','Database','APPMANAGER');
  FConn.DatabaseName := FDatabase;
  FDbUser := 'username';
  FDbPassword := 'userpassword';
  FConn.UserName := FDbUser;
  FConn.Password := FDbPassword;
end;

And the connection handlers:

function TAppDbHandler.OpenConnection: boolean;
begin
  try
    Result := false;
    CloseConnection;
    FConn.LoginPrompt := false;
    FConn.Open;
    Result := FConn.Connected;
  except
    //placeholder
  end;
  if not Result then
    MessageDlg('Could not connect to database!', mtError, [mbOk], 0);
end;
procedure TAppDbHandler.CloseConnection;
begin
	if FConn.Connected then
	  FConn.Close;
end;

But I have not been able to check this because now compilation stops at a strange error regarding bitmaps...

I am using a QRcode generation unit I got from GitHub long time ago (https://github.com/foxitsoftware/DelphiZXingQRCode) and it deals with TBitmap but when I test compile my app I get this in the main form code:

var
  QR_BMP_SSM: TBitMap;
  ...
begin
  ...
(!)  QR_BMP_SSM := TBitMap.Create; //<== Error here

Error messages:
Compile Project, Target: lib\x86_64-win64\AppManager.exe: Exit code 1, Errors: 2 FormAppManager.pas(410,25) Error: identifier idents no member "Create" FormAppManager.pas(410,25) Fatal: Syntax error, ";" expected but "identifier CREATE" found

But after some digging around I removed this error by moving Graphics to next to last in the form pas file uses list....
Strange.

Now I have gotten the project to build so I should start debugging.

But when I try to run it from within Lazarus (hit the green arrow button) then a pop-up image appears:

------------------------------------------------------------------------------ Enable Dwarf 2 (-gw)?
The project does not write debug info in Dwarf format. The "FpDebug internal Dwarf-debugger" supports only Dwarf.
[Enable Dwarf2 with sets] [Enable Dwarf2 (-gw)] [Enable Dwarf3 (-gw3) [Cancel] ------------------------------------------------------------------------------

What am I supposed to do with this?

Bo Berglund Developer in Sweden

Reply by email

Bo Berglund
to Bo Berglund
Bo Berglund wrote 13 lines
Now I have gotten the project to build so I should start debugging. But when I try to run it from within Lazarus (hit the green arrow button) then a pop-up image appears: ------------------------------------------------------------------------------ Enable Dwarf 2 (-gw)? The project does not write debug info in Dwarf format. The "FpDebug internal Dwarf-debugger" supports only Dwarf. [Enable Dwarf2 with sets] [Enable Dwarf2 (-gw)] [Enable Dwarf3 (-gw3) [Cancel] ------------------------------------------------------------------------------ What am I supposed to do with this?

After reading up on the Wiki:
https://wiki.freepascal.org/DWARF

I selected Dwarf2 with sets.

Then rebuilt and tried to run, but I get an immediate fatal exception! This happens both when starting within Lazarus and from the exe file itself. Message:
-------------------------------------------------------- | Project AppManager raised exception class 'External: | | ACCESS VIOLATION' with message: | | Access violation reading from address | | $0000000000000040 | | | | At adress 1000096AD [OK] | --------------------------------------------------------

How can I find the cause of this exception?

Bo Berglund Developer in Sweden

Reply by email

Martin Frb
to Bo Berglund
Bo Berglund wrote 16 lines
Then rebuilt and tried to run, but I get an immediate fatal exception! This happens both when starting within Lazarus and from the exe file itself. Message: -------------------------------------------------------- | Project AppManager raised exception class 'External: | | ACCESS VIOLATION' with message: | | Access violation reading from address | | $0000000000000040 | | | | At adress 1000096AD [OK] | -------------------------------------------------------- How can I find the cause of this exception?

Did the debugger jump to any source code?
What does the "Callstack" window show?

If neither shows anything, the error might be in the RTL or kernel. What does the assembler win show?

Give the data from the error message, I guess there is a nil pointer deref (e.g. access to a field in an object that is nil)

Reply by email

Bo Berglund
to Martin Frb
Martin Frb wrote 26 lines
On 11/02/2023 12:23, Bo Berglund via lazarus wrote: > > > Then rebuilt and tried to run, but I get an immediate fatal exception! > This happens both when starting within Lazarus and from the exe file itself. > Message: > -------------------------------------------------------- > | Project AppManager raised exception class 'External: | > | ACCESS VIOLATION' with message: | > | Access violation reading from address | > | $0000000000000040 | > | | > | At adress 1000096AD [OK] | > -------------------------------------------------------- > > How can I find the cause of this exception? > Did the debugger jump to any source code? What does the "Callstack" window show? If neither shows anything, the error might be in the RTL or kernel. What does the assembler win show? Give the data from the error message, I guess there is a nil pointer deref (e.g. access to a field in an object that is nil)

When I wrote my last post I had problems finding a location to put a breakpoint so I could step through the code.

But later I found by trial-and-error where the exception was coming from in general. And where I could put a breakpoint to start stepping from.

It is from a logging component I have used on Delphi since more than 20 years. When it starts its logging it is supposed to read the application version info and write a start entry to today's log including the application version etc.

When I moved some applications from Delohi on Windows to Lazarus on Linux I had to make a clone of the logging component where a lot of dependencies on external components for reading version etc was removed and instead the Lazarus/fpc handling was taken into account. Then it has worked fine on Lazarus at least on Linux.

When porting the current application I changed the Class name for the logging component correspondingly and hoped it would be enough, but unfortunately it is not. I don't know where I should fix it yet and I don't have time to do it now when I am busy just porting the handler application.

So for now I commented out all calls to the logger creation (where the exception happened) and its use so I can move on.

Success!
This has brought me to the verification that now the ported app is actually working regarding access to the MSSQL database! :-) The DB conmnection is needed in order to start up the app at all, without a database verified login it just shuts down. But it IS working with the MSSQLSERVER just fine so far.

But I have another problem I will describe in a new thread...

Bo Berglund Developer in Sweden

Reply by email