Alex Zhulin
Alex Zhulin

Reputation: 1305

Delphi ADODB Create dbf table

I have to create dbf file in my Delphi application.

For this case I use ADODB.

Connection string:

const
  ConnStringDBF =
  'Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=%s;';
  //'Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=dBASE III;OLE DB Services=-1;Data Source=%s;';
  //'Driver={Microsoft Visual Foxpro Driver};SourceType=DBF;SourceDB=%s;';
  //Driver='Microsoft.ACE.OLEDB.12.0;Data Source=%s;';

And procedure (which doesn work):

procedure InsertRecordInDBF(file_name: string; DbfRecord: TDbfRecord);
var
  ADOConnDBF  : TADOConnection;
  ADOCommand : TADOCommand;
begin
  ADOConnDBF := TADOConnection.Create(nil);
  ADOConnDBF.ConnectionString := Format(ConnStringDBF, [data_dir]);
  ADOCommand := TADOCommand.Create(nil);
  ADOCommand.Connection := ADOConnDBF;
  ADOCommand.CommandText := 'Create Table ' + file_name + ' ('
                        + 'NUMBER CHAR(11)'
                        + ')'
                        ;
  ADOCommand.Execute;
end;

The error is:

raised exception class EOleException with message '[Microsoft][Драйвер ODBC dBase] Ошибка синтаксиса при определении поля'.

Which means: syntax error in translation from Russian.

But this CommandText works perfectly:

  ADOCommand.CommandText := 'Create Table ' + file_name + ' ('
                        + 'NUMBER_ CHAR(11)'
                        + ')'
                        ;

The difference in the name of the field (Number_ instead of Number)

How can I create table with field Number?

Maybe I need different connection string?

Upvotes: 2

Views: 3269

Answers (2)

Oleg
Oleg

Reputation: 823

Use VFPOLEDB provider with the following connection string:

Provider=VFPOLEDB.1;Data Source=%s;Password="";Collating Sequence=MACHINE

In this case your CREATE TABLE clause will work fine.

Upvotes: 0

Alex Zhulin
Alex Zhulin

Reputation: 1305

It seems that I found solution for this problem. The right syntax is:

  ADOCommand.CommandText := 'Create Table ' + file_name + ' ('
                        + '[NUMBER] CHAR(11)'
                        + ')'
                        ;

I added [] to the name of the field

Upvotes: 2

Related Questions