Tuesday, December 21, 2004

Console application with ADO in Delphi

I am working on this project which need little plug-in to be written. Main thing is that the plugin should be console application and must have database access support. So, I started writing a code in Delphi but couldn't get things work properly. No matter what I tried, I got memory access violation. With little research, I found that CoInitialization is needed to be called before any ADO component is initialized and used. Below is the source of sample console application with ADO support in Delphi;
program Project1;
{$APPTYPE CONSOLE}

uses
 ADOdb,
 ActiveX;

var
 conMain : TADOConnection;
 tab1 : TADOTable;

begin
 CoInitialize(nil);
 conMain := TADOConnection.Create(nil);
 tab1 := TADOTable.Create(nil);

 conMain.ConnectionString := '{your connection string here}';
 conMain.LoginPrompt := False;
 conMain.Open;

 tab1.Connection := conMain;
 tab1.TableName := '{your table name here}';
 tab1.Active := True;
 tab1.Recordset.MoveFirst;

while not tab1.Recordset.EOF do begin writeln(tab1.Recordset.Fields[1].Value); tab1.Recordset.MoveNext; end;

CoUninitialize; end.

8 comments:

Anonymous said...

Your posting on using ADO components in a Delphi console application is absolutely GREAT!!!

This is the first posting I could find that truly helped me solve my problem.

Thank you for posting this.

Anonymous said...

Was very helpful for me too, thank you

Anonymous said...

Excellent!
Exactly what I was looking for that I couldn't find anywhere else.

SoulIntruder said...

Thank you Milan, I had problem with this too.

Anonymous said...

This is a great research. I am trying to do a Delphi CGI with ADO connection and it has been a pain in the ass. Before seeing this post i was using delphi types files to get it working. But this post....saved my life. Good job dude !
Alin

Millan said...

Great about this Post. Thanks for sharing all this Info. Delphi

spike said...

Hi,

I just wanted to say that I really enjoyed your blog and this post. You make some very informative points. Keep up the great work!

-
Delphi development

Anonymous said...

Thanks a lot