请教高手:如何用Delphi创建一个空的Access数据库?

解决方案 »

  1.   


      SConnectionString       = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'
                                    +'Jet OLEDB:Database Password=%s;';function GetTempPathFileName():string;
    var
      SPath,SFile:array [0..254] of char;
    begin
      GetTempPath(254,SPath);
      GetTempFileName(SPath,'~SM',0,SFile);
      result:=SFile;
      DeleteFile(result);
    end;function CreateAccessFile(FileName:String;PassWord:string=''):boolean;
    //建立Access文件,如果文件存在则失败
    var
      STempFileName:string;
      vCatalog:OleVariant;
    begin
      STempFileName:=GetTempPathFileName;
      try
        vCatalog:=CreateOleObject('ADOX.Catalog');
        vCatalog.Create(format(SConnectionString,[STempFileName,PassWord]));
        result:=CopyFile(PChar(STempFileName),PChar(FileName),True);
        DeleteFile(STempFileName);
      except
        result:=false;
      end;
    end;
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Db, ADODB, comobj;type
      TForm1 = class(TForm)
        ADO: TADOConnection;
        ADOQuery1: TADOQuery;
        Button2: TButton;
        Button3: TButton;
        Memo1: TMemo;
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button2Click(Sender: TObject);
    var Source,str_sql:string;
        CreateDB:Variant;
    begin
      //判断数据库是否已经存在
      if FileSearch('DB.MDB',ExtractFileDir(Application.ExeName)+'\')<>'' then
      begin
        Application.MessageBox('数据库已经存在!','提示',64);
        exit;
      end;
      
      // 创建数据库
      Source:=' Provider=Microsoft.Jet.OLEDB.4.0; '
             +' Data Source= '
             +ExtractFileDir(Application.ExeName)
             +'\DB.MDB';
      try
        CreateDB:=CreateOleObject('ADOX.Catalog');
        CreateDB.Create(Source);
        Application.MessageBox('数据库创建成功','提示',64);
      except
        Application.MessageBox('数据库创建失败','提示',64);
      end;  //创建数据库中的表
      ADO.ConnectionString:=Source;
      ADO.Connected:=true;  ADOQuery1.Connection:=ADO;
      ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
      str_sql:=' create table T ( ID char(10) ) ';
      ADOQuery1.SQL.Add(str_sql);
      try
        ADOQuery1.ExecSQL;
        Application.MessageBox('数据表创建成功','提示',64);
      except
        Application.MessageBox('数据表创建失败','提示',64);
      end;end;procedure TForm1.Button3Click(Sender: TObject);
    var DBName,s_sql:string;
    begin
    //删除数据表
      ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
      s_sql:=' drop table T ';
      ADOQuery1.SQL.Add(s_sql);
      try
        ADOQuery1.ExecSQL;
        Application.MessageBox('数据表删除成功','提示',64);
      except
        Application.MessageBox('数据表删除失败','提示',64);
      end;// 删除数据库
      DBName:=ExtractFileDir(Application.ExeName)+'\DB.MDB';
      try
        DeleteFile(DBName);
        Application.MessageBox('数据库删除成功!','提示',64);
      except
        Application.MessageBox('数据库删除失败!','提示',64);
      end;
    end;end.
      

  3.   

    http://expert.csdn.net/Expert/topic/749/749317.xml?temp=.9471399