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.

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

Unknown said...

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

Anonymous said...

Thanks a lot

Anonymous said...

if u want to add usr and password came from encrypt ini file add :

Var
.....
DocILCnn : TADOConnection;
CatalogQry : TADOQuery;
InsBlobRecCmd : TADOCommand;
GeneralQry : TADOQuery;
SqlConnectionStr : String;
SqlConnectionUser : String;
SqlConnectionPassword : String;
WinTempDir : String;
....

Procedure LoadIniParams;
Var
LocalConnection : String;
IniFileName : String;
ParamsIni : TIniFile;
begin
SqlConnectionStr := '';
SqlConnectionUser := '';
SqlConnectionPassword := '';
IniFileName := GetIniFileName (make your proc);
If Not FileExists(IniFileName) Then
Exit;

ParamsIni := TIniFile.Create(IniFileName);
Try
SqlConnectionStr := Trim(ParamsIni.ReadString('SQLSec','SqlConnectionString',LocalConnection));
SqlConnectionUser := Trim(ParamsIni.ReadString('SQLSec','SQLUserName',''));
SqlConnectionPassword := Trim(ParamsIni.ReadString('SQLSec','SQLPassWord',LocalConnection));
SqlConnectionPassword := Trim(DecryptString(SqlConnectionPassword,SQLEncryptKey));
Finally
ParamsIni.Free;
End;
end;


type
TMyConsoleAdoCnn = class
public
class procedure DocILCnnWillConnect(Connection: TADOConnection;
var ConnectionString, UserID, Password: WideString;
var ConnectOptions: TConnectOption; var EventStatus: TEventStatus);
end;

class procedure TMyConsoleAdoCnn.DocILCnnWillConnect(Connection: TADOConnection;
var ConnectionString, UserID, Password: WideString;
var ConnectOptions: TConnectOption; var EventStatus: TEventStatus);
begin
If Trim(SqlConnectionStr) = '' Then
Exit;
ConnectionString := SqlConnectionStr;
if Trim(SqlConnectionUser) <> '' then
begin
UserID := SqlConnectionUser;
Password := SqlConnectionPassword;
end;
end;


begin
....

LoadIniParams;

....
DocILCnn := TADOConnection.Create(nil);
DocILCnn.LoginPrompt := False;
DocILCnn.OnWillConnect := TMyConsoleAdoCnn.DocILCnnWillConnect;

....
end