怎样在程序中自动创建一个access2000数据库,并给它加上密码保护?
access2000的mdb库什么时候需要压缩?

解决方案 »

  1.   

    建库与表的例子自己看看吧,其中建表部分有问题,建议还是用SQL建表的好.
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, comobj,adox_tlb,Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      Catalog: _Catalog;
      Table: _Table;
      Index : _Index;
      //FKey : _key;
      strCon:string;//¶¨ÒåÁ¬½Ó×Ö·û´®
      yourname:string;
      yourpwd:string;
    begin
        yourname:=trim(edit1.Text);
        yourpwd:=trim(edit2.text);
        Catalog := CoCatalog.Create;
        strCon := 'Provider=Microsoft.Jet.OleDB.4.0;'
        //ͨ¹ýJet OleDbÖ±½Ó²Ù×÷AccessÊý¾Ý¿â
        +'Data Source=c:\windows\desktop\'+yourname+'.mdb;'
        //Êý¾Ý¿âλÖÃ
        +'Jet OLEDB:Engine Type=5;'
        //Jet 4.x¸ñʽ£¬ÈçΪ4£¬ÔòJet 3.x¸ñʽ
        +'Locale Identifier=0x0804;'
         //Ö§³Ö¼òÌåÖÐÎÄ£¨Ò»¶¨ÒªÓУ©
        +'Jet OLEDB:Database Password='+yourpwd;//密码;
        //¼ÓÈëÃÜÂë
        Catalog.Create(strCon); //½¨Á¢Êý¾Ý¿â
        {½¨Á¢Êý¾Ý±íºÍË÷Òý}
        Catalog.Set_ActiveConnection(strCon);
        //Á¬½Óµ½Êý¾Ý¿â    with Catalog do
        begin //½¨Á¢Êý¾Ý±í
        Table:= CoTable.Create(); //½¨Á¢TableʵÀý    with Table do
        begin
        Name := 'MyTable1';    //½¨±í MyTable1
        Table.ParentCatalog := Catalog ;
        Columns.Append('ID',adInteger,8);
        
        Columns.Item['ID'].Properties.Item['AutoIncrement'].Value := true;
        
        Columns.Append('Name',adVarWChar,40);
        Columns.Append('Parent_ID',adInteger,8);
        Columns.Item['Parent_ID'].Properties['Default'].Value := 0;
                                               
        Columns.Append('Sort_ID',adInteger,8);
        Columns.Append('Counter',adInteger,8);
        Columns.Item['Counter'].Properties.Item['Default'].Value := 0;    //Êý¾ÝÀàÐÍÏê¼ûMDAC SDK
        Tables.Append(Table);  //½¨±í MyTable1    Index := CoIndex.Create() as _Index; //½¨Á¢Ë÷Òý
        with Index do
        begin
        Name:='Idx1';
        PrimaryKey := True ;
        Unique := True;
        Columns.Append('ID',adInteger,8);
        _Release;
        end;
        Table.Indexes.Append(Index,EmptyParam);    Table._Release;
        Table:= CoTable.Create();
        end;           //with table do
        end;           //with catalog doend;
      

  2.   

    有乱码?看这个好了,其中的'要注意可能有错,自己改改.
    uses ComObj;function CreateDatabase(mDatabaseName: string): Boolean;
    var
      CreateAccess: OLEVariant;
    begin
      Result := False;
      try
        CreateAccess := CreateOleObject('ADOX.Catalog');
        try
          CreateAccess.Create('Provider=Microsoft.Jet.OLEDB.3.51;Data Source=%s%s.mdb';Jet OLEDB:Database Password='+密码+'')
        finally
          CreateAccess := Unassigned;
        end;
      except
        Exit;
      end;
      Result := True;
    end;
      

  3.   

    function CreateDatabase(mDatabaseName: string): Boolean;不要了.
    就看下面的:var
      CreateAccess: OLEVariant;
    begin
      Result := False;
      try
        CreateAccess := CreateOleObject('ADOX.Catalog');
        try
          CreateAccess.Create('Provider=Microsoft.Jet.OLEDB.3.51;Data Source=%s%s.mdb';Jet OLEDB:Database Password='+密码+'')
        finally
          CreateAccess := Unassigned;
        end;
      except
        Exit;
      end;
      Result := True;
    end;
      

  4.   

    非常感谢chenjiong(准程序员:升级中....)的大力帮助,已经解决第一个问题;我的第二个问题呢: access2000的mdb库什么时候需要压缩?