tags:

views:

193

answers:

3

First of all, I must state that I'm a complete newb when it comes to Delphi, allthough I did some Turbo Pascal programming in school, some fourteen years ago...

I have a comercial Delphi program which uses dBase database and BDE for accessing them. I basically need to interface another application written in C# to this database, to be able to do SQL operations such as select, insert, update and delete.

Unfortunaltely using OLEDB against dBase results in broken indexes, only native BDE apps seem to be able to safely access the data.

The general idea was to create a simple Delphi console application which could read SQL statements from standard input (Read/ReadLn) and output CSV data to standard output (WriteLn).

How would I go about doing this?

I have successfully gotten simple TTable-access to work, with the following code:

tbl := TTable.Create(nil);

tbl.DatabaseName := 'Exceline';
tbl.TableName := 'KUNDE.DBF';
tbl.Active := True;

WriteLn(tbl.RecordCount);

tbl.Active := False;

Is there a way I could achieve the same but by executing direct SQL statements instead?

+8  A: 

You can do the same using TQuery component:

qry := TQuery.Create(nil);

qry.DatabaseName := 'Exceline';
qry.SQL.Add('SELECT COUNT(*) AS CNT FROM KUNDE');
qry.Active := True;

WriteLn(qry.FieldByName('CNT').AsString);

qry.Active := False;
Serg
Thank's for your reply, I actually got around to try something similar and it worked perfectly. How does AsString handle null values?
thomask
@thomask - returns an empty string as I remember. You can check null value using TField.IsNull method, ex [if qry.FieldByName('CNT').IsNull then ...]
Serg
+3  A: 

As Serg already wrote: You can use a tquery object to execute sql queries on dbase tables. But beware: The way you propose to do that - passing a sql query to a program via stdin and having it return the results on stdout - is very slow on Windows.

Also, you will have to add additional commands to your program for returning the data in batches if the result of a query is huge. It's probably easier and will give you much better performance to write a COM server in Delphi an use that from C#.

And one last point: The BDE has not been supported by Borland/Codegear/Embarcadero for several years. It still works but it gets harder and harder to keep it that way, especially with newer Windows versions than XP. One alternative might be tdbf (see sourceforge), but I have not enough experience with that to give you an informed opinion on it.

dummzeuch
This sounds like a good suggestion, do you by any chance have any pointers to how I can get started writing a COM server?
thomask
+1 for tdbf. I would go that way
Marco van de Voort
A: 

Since the BDE has not been maintained since it was deprecated 10 years ago:

Did you consider Advantage Database Server? It is a server that can access dBase, Clipper and other xBase

It works really nice, and there is an .NET Data Provider available for it.

That would make your solution path a lot less complex.

--jeroen

Jeroen Pluimers