我用access2000做好一个数据库aa,我想在一个工程中动态创建一个表bb。请问需要在工程中添加什麽控件连接到数据库aa,之后再怎样写代码动态创建一个表bb!我想动态的(form创建时)在窗体中创建多个edit控件,并且可以根据edit控件的多少来决定form的大小,请问如何实现,谢谢!

解决方案 »

  1.   

    1.
    acSec: TADOConnection;
    aqSec: TADOQuery;
    ...
    procedure TfrmSec.btnAddClick(Sender: TObject);
    var
      sSql: string;
      tName: TStrings;
    begin
      tName := TStringList.Create;
      acSec.GetTableNames(tName, false);
      if tName.IndexOf('staff') >= 0 then acSec.Execute('drop table staff');
      sSql := 'create table staff(ID AutoIncrement Primary Key, ' +  //Integer identity; Counter
                                 'sName Text(8) not Null, ' +
                                 'iAge Byte Default 0, ' +
                                 'sCompany Text(32) Null Default "", ' +  {虽然在设计表中看到不能为空,实际可以}
                                 'sSalary Decimal(8,2) Default 0, ' +
                                 'bVisible Bit Default True, ' +  {是/否}
                                 'sDes Memo Default "", ' +  {备注}
                                 'mDate Date Default Date())';
                                 {动态设主键:'alter table stableb add primary key (id)'}  aqSec.Close;
      aqSec.SQL.Text := sSql;
      try
        aqSec.ExecSQL;
        MessageBox(self.Handle, '创建staff成功', '提示', mb_IconInformation + mb_Ok);
      except
        MessageBox(self.Handle, 'fail!', '提示', mb_IconInformation + mb_Ok);
      end;
      tName.Free;
    end;2.我没有好办法
      

  2.   

    请问您一开始定义的acSec: TADOConnection;aqSec: TADOQuery;是什莫意思,是别名吗?谢谢了第二个问题谁能帮我看一下!
      

  3.   

    No.1:
    ADOQuery.Connection := ADOConnection;//ADOConnection 已经连接
    with ADOQuery do
    begin
      if Active then
        Active := False;
      SQL.Clear;
      SQL.Add('create table bb(no char(4),name char(8))';
      ExecSQL;
    end;
    No.2:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, ADODB, StdCtrls;type
      TForm1 = class(TForm)
        ADOQuery1: TADOQuery;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        function GetCount: Integer;
        procedure SetCount(const Value: Integer);
        Procedure CreateEdit(vName : String;vLeft,vTop,vHeight,vWidth:Integer;vParent : TWinControl);
        { Private declarations }
      public
        { Public declarations }
        property FCount :Integer Read GetCount Write SetCount;
      end;Const
      FHeight = 21;
      FWidth = 120;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      FCount := 13;
    end;function TForm1.GetCount: Integer;
    begin
      //..
    end;procedure TForm1.SetCount(const Value: Integer);
    var
      vIndex : Integer;
      vTop,vLeft : Integer;
    begin
      //..
      vTop := 0;
      vLeft := 0;
      for vIndex := 0 to Value do
      begin
        CreateEdit('Create Edit' + IntToStr(vIndex),vLeft,vTop,FHeight,FWidth,Self);
        vLeft := vLeft + 125;
      end;
      if Self.Width < vLeft + 125 then
        Self.Width := vLeft + 125 ;
    end;procedure TForm1.CreateEdit(vName: String; vLeft, vTop, vHeight,
      vWidth: Integer; vParent: TWinControl);
    var
      vEdit : TEdit;
    begin
      //..
      vEdit := TEdit.Create(Nil);
      vEdit.Parent := vParent;//
      vEdit.Left := vLeft;
      vEdit.Top := vTop;
      vEdit.Height := vHeight;
      vEdit.Width := vWidth;
    end;end.
      

  4.   

    http://community.csdn.net/Expert/topic/3008/3008997.xml?temp=.3814966