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'的意思是創(chuàng)建一個(gè)叫:'No1'的鍵,'PRIMARY KEY'是將其}
{ 設(shè)置為主鍵,'COUNTER'是自動(dòng)編號 }
{ 'TreeNo1 INTEGER'是創(chuàng)建一個(gè)叫'TreeNo1'的'INTEGER'整數(shù)型鍵 }
{ 'ItemName1 TEXT'是創(chuàng)建一個(gè)叫'ItemName1'的'TEXT'字符型鍵 }
{ 'HasParent1 BIT'是創(chuàng)建一個(gè)叫'HasParent1'的'BIT'布爾型鍵 }
{ 以下省略鍵名解釋直接解釋類型 }
{ 'IMAGE'是OLE類型可存儲任何東西圖片,文件,流 }
{ 'DATETIME'是時(shí)間類型 }
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數(shù)據(jù)庫
+'Data Source=c:\windows\desktop\'+yourname+'.mdb;'
//數(shù)據(jù)庫位置
+'Jet OLEDB:Engine Type=5;'
//Jet 4.x格式,如為4,則Jet 3.x格式
+'Locale Identifier=0x0804;'
//支持簡體中文(一定要有)
+'Jet OLEDB:Database Password='+yourpwd;//修改密碼也在此;
//加入密碼
Catalog.Create(strCon); //建立數(shù)據(jù)庫
{建立數(shù)據(jù)表和索引}
Catalog.Set_ActiveConnection(strCon);
//連接到數(shù)據(jù)庫
with Catalog do
begin //建立數(shù)據(jù)表
Table:= CoTable.Create(); //建立Table實(shí)例
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;
//數(shù)據(jù)類型詳見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里動(dòng)態(tài)創(chuàng)建一個(gè)access表
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('create table tmp_rece( 學(xué)號 char(20),姓名 char(20),班級 char(20),dat datetime)');
adoquery1.execsql;
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from tmp_rece');
adoquery1.Open;