RaelB 4 Posted July 15, 2021 Can one include a TDataModule with DB components in a console application? I tried to do so, and was encountering some errors in the IDE (Delphi 10.3). Well that just might be a temporary thing in the IDE, or is this something one should not/can not do? Thanks Share this post Link to post
aehimself 399 Posted July 15, 2021 A TDataModule is basically a descendant of TComponent, there is nothing stopping you adding one to your console application. You'll have to manually create and destroy it though. I just checked and could add a DataModule in my 10.4.2 IDE. Share this post Link to post
Anders Melander 1815 Posted July 15, 2021 1 hour ago, RaelB said: I tried to do so, and was encountering some errors in the IDE What errors? Share this post Link to post
corneliusdavid 220 Posted July 15, 2021 3 hours ago, RaelB said: Can one include a TDataModule with DB components in a console application? Yes, absolutely! If you want to write a server app or a web module, this is the best way to go as debugging a console app is very simple and the data module can be shared between the two projects. I do this quite frequently. Here's a sample console project loading a data module: program ConsoleProjectTest; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, udmUnit1 in 'udmUnit1.pas' {dmUnit1: TDataModule}; begin try dmUnit1 := TdmUnit1.Create(nil); try // do stuff with data module finally dmUnit1.Free; end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. Hope that helps! Share this post Link to post
aehimself 399 Posted July 15, 2021 20 minutes ago, corneliusdavid said: If you want to write a server app or a web module, this is the best way to go as debugging a console app I started to write my applications as a single class some years ago and never turned back. Want to start from console? TMyApp.Create; From a Windows service? TMyApp.Create; From the Mt. Everest? TMyApp.Create; You'll have to refine logging for sure but this is the easiest way I ever experienced to write self-containing, easy to debug code. I even took this one step further by adding some code to the .dpr of my Delphi services: if you start with the /console parameter, it starts as a console app instead of a service. Share this post Link to post