分享

delphi给access数据库创建新表的方法

 晶晶suky 2011-02-17

ADOQ1.SQL.Text:='CREATE   TABLE   BookTable   '
              +'(No1   COUNTER   PRIMARY   KEY,'
              +'ItemName1   TEXT,TreeNo1   INTEGER,HasParent1   BIT,'
              +'ParentNo1   INTEGER,ItemType1   INTEGER,ItemText1   IMAGE,'
              +'ItemAttr1   IMAGE,ADDTime1   DATETIME)';
{ 注释:'BookTable'是表名 }
{ 'No1   COUNTER   PRIMARY   KEY'的意思是创建一个叫:'No1'的键,'PRIMARY   KEY'是将其}
{ 设置为主键,'COUNTER'是自动编号 }
{ 'TreeNo1   INTEGER'是创建一个叫'TreeNo1'的'INTEGER'整数型键 }
{ 'ItemName1   TEXT'是创建一个叫'ItemName1'的'TEXT'字符型键 }
{ 'HasParent1   BIT'是创建一个叫'HasParent1'的'BIT'布尔型键 }
{ 以下省略键名解释直接解释类型 }
{ 'IMAGE'是OLE类型可存储任何东西图片,文件,流 }
{ 'DATETIME'是时间类型 }

unit   Unit1;

interface    
uses
      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   do
     end;    
end.
//在DELPHI里动态创建一个access表
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('create table tmp_rece( 学号 char(20),姓名 char(20),班级 char(20),dat datetime)');
adoquery1.execsql;
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from tmp_rece');
adoquery1.Open;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多