Reputation: 1542
I have a simple Console Application written using Delphi XE2.
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Data.DBXMSSQL,
Data.DB,
Data.SqlExpr;
var
myConnection: TSQLConnection;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
myConnection := TSQLConnection.Create(nil);
myConnection.DriverName := 'MSSQL';
myConnection.GetDriverFunc := 'getSQLDriverMSSQL';
myConnection.LibraryName := 'dbxmss.dll';
myConnection.VendorLib := 'sqlncli10.dll';
myConnection.LoginPrompt := False;
myConnection.Params.Clear;
myConnection.Params.Add('drivername=MSSQL');
myConnection.Params.Add('schemaoverride=%.dbo');
myConnection.Params.Add('hostname=myserver');
myConnection.Params.Add('database=mydb');
myConnection.Params.Add('blobsize=1');
myConnection.Params.Add('localcode=0000');
myConnection.Params.Add('isolationlevel=ReadCommited');
myConnection.Params.Add('os authentication=True');
myConnection.Params.Add('prepare sql=False');
myConnection.Connected := true;
Writeln('myConnection Is connected');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
When I run this, I get this error:
'DBX Error: Driver could not be properly initialized. Client library may be missing, not installed properly, of the wrong version, or the driver may be missing from the system path.'
If I add a VCL form to the application, I get the pop-up about enabling the "Visual Component Library" framework, which I say No to, and without doing anything else, now when I run the application I get 'myConnection Is connected'.
The only difference I can see is in the uses clause:
uses
System.SysUtils,
Data.DBXMSSQL,
Data.DB,
Data.SqlExpr,
Unit1 in 'Unit1.pas' {Form1}
If I remove the Unit1 in 'Unit1.pas' {Form1} from the uses clause then the app fails to connect.
I do not want to include a form in my console application, so what is happening to the program when I do such that it works?
Upvotes: 6
Views: 1411
Reputation: 80325
VCL internally calls CoInitialize during initialization (initializes the COM library on the current thread), but console application doesn't (the same problem occurs in worker thread requiring some COM-stuff).
Just make CoInitialize call in the beginning of program or in initialization section, and CoUninitialize after the work.
http://docwiki.embarcadero.com/RADStudio/Rio/en/DbExpress_Database_Specific_Information
Upvotes: 13