1. 程式人生 > >動態建立DBF資料檔案

動態建立DBF資料檔案

1、SQL建表:
   query1.close;
   query1.sql.text:='create table "mytable.dbf" (n1 char(10))';
   query1.execsql;
   DELPHI建表:
   table1.close;
   table1.fielddefs.clear;
   table1.fielddefs.add('n1',ftstring,10,false);
   table1.createtable;
2、刪除記錄
   uses bde;//加入bde單元
//////
   dbipacktable();函式 


建立表並新增資料
  with Table1 do begin
    Active := False;
    DatabaseName := 'DBDEMOS';
    TableType := ttParadox;
    TableName := 'CustInfo';
    if not Table1.Exists then begin
      with FieldDefs do begin
        Clear;
        with AddFieldDef do begin
          Name := 'Field1';
          DataType := ftInteger;
          Required := True;
        end;
        with AddFieldDef do begin

          Name := 'Field2';
          DataType := ftString;
          Size := 30;
        end;
      end;
      CreateTable;
    end;
  end;
  Table1.Open;
  table1.AppendRecord([111,'1111']);
  table1.AppendRecord([222,'2222']);
  table1.AppendRecord([333,'3333']);
  table1.AppendRecord([444,'4444']);

刪除記錄
  Table1.Delete;
 
另外可以使用ado建立:(例子如下)
//ado建立dbf
procedure TForm1.Button1Click(Sender: TObject);
var
DBPath: String;
begin
DBPath:='e:/dfwtest/test7';//你的資料庫路徑目錄;
ADOConnection1.ConnectionString:='Provider=MSDASQL.1;'
                                 +'Persist Security Info=False;'
                                 +'Extended Properties='
                                 +'"Driver={Microsoft Visual FoxPro Driver};'
                                 +'UID=;'
                                 +'SourceDB='+ DBPath + ';'
                                 +'SourceType=DBF;'
                                 +'Exclusive=No;'
                                 +'BackgroundFetch=Yes;'
                                 +'Collate=Machine;'
                                 +'Null=Yes;'
                                 +'Deleted=Yes;"';
AdoConnection1.Open;
AdoQuery1.Sql.Text := 'create table "aaa.dbf" (aaa char(2))';
//AdoQuery1.Sql.Text := 'create table "ddd.dbf" (aaa char(2)),fff float(3),eee numeric (4,1),torf boolean ,rdate date)';
AdoQuery1.ExecSql;
end;
//ado開啟dbf
procedure TForm1.Button3Click(Sender: TObject);
var
DBPath: String;
begin
DBPath:='e:/dfwtest/test7';//你的資料庫路徑;
ADOConnection1.ConnectionString:='Provider=MSDASQL.1;'
                                 +'Persist Security Info=False;'
                                 +'Extended Properties='
                                 +'"Driver={Microsoft Visual FoxPro Driver};'
                                 +'UID=;'
                                 +'SourceDB='+ DBPath + ';'
                                 +'SourceType=DBF;'
                                 +'Exclusive=No;'
                                 +'BackgroundFetch=Yes;'
                                 +'Collate=Machine;'
                                 +'Null=Yes;'
                                 +'Deleted=Yes;"';
AdoQuery1.close;
AdoQuery1.sql.clear;
AdoQuery1.sql.Text := 'select * from ddd.dbf';
adoquery1.Open;
end;