HOW to connect to SQLDB from commandline

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
carson

HOW to connect to SQLDB from commandline

Post by carson » Mon 02 May 2005 14:17

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.

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Wed 04 May 2005 07:59

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.

Post Reply