|
用Delphi创建一个空的Access数据库
- unit Unit1;
-
- interface
-
- uses
- 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.
复制代码
|
|