Jump to content
RaelB

Can one include a TDataModule in a console application?

Recommended Posts

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

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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×