Page 1 of 1

HOW to connect to SQLDB from commandline

Posted: Mon 02 May 2005 14:17
by carson
why do i always get runtime errors ??

greeting carson :roll:


program foo;

{$APPTYPE CONSOLE}

uses
Windows,
DB,
MemDS,
DBAccess,
MSAccess,
OLEDBAccess,
SysUtils;

Var
MSSQL:TMSConnection;
mTABLE:TMSTABLE;
i:integer;

begin
MSSQL := TMSConnection.Create(Nil);
MSSQL.Database := 'mydb';
MSSQL.Username := 'Admin';
MSSQL.Password := 'foo007';
MSSQL.Server := '100.100.10.10';
MSSQL.Authentication := auWindows;
MSSQL.Connected := True;

mTable := TMSTAble.Create(Nil);
mTable.Connection := MSSQL;
mTable.TableName := 'mycomputer';
mTable.Active := True;

mTable.First;

for i := 0 to mTable.RecordCount-1 do
begin
writeln(mTable.FieldByName('computername').Text);
mTable.Next;
end;

mTable.Active := False;
MSSQl.Connected := False;
mTable.Free;
MSSQL.Free;

end.

Posted: Wed 04 May 2005 07:59
by Ikar
If you would like to use SDAC in service, console or just at separate thread you need call CoInitialize for each thread. Also remember to call CoUnInitialize at the end of thread.