菜
菜鸟黄
Unregistered / Unconfirmed
GUEST, unregistred user!
回赠大富翁论坛对我的水平提高,现在特意发布<动态创建Access数据库>的完整代码<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids, ExtCtrls, DBCtrls,<br> ComObj, dbtables;<br><br>type<br> TForm1 = class(TForm)<br> DataSource1: TDataSource;<br> AD: TADOQuery;<br> ADOConnection1: TADOConnection;<br> Panel1: TPanel;<br> DBGrid1: TDBGrid;<br> Edit1: TEdit;<br> Button1: TButton;<br> Button2: TButton;<br> Panel2: TPanel;<br> DBGrid2: TDBGrid;<br> Panel3: TPanel;<br> Button3: TButton;<br> Label1: TLabel;<br> Label2: TLabel;<br> ComboBox1: TComboBox;<br> DataSource2: TDataSource;<br> AC: TADOQuery;<br> DBNavigator1: TDBNavigator;<br> ComboBox2: TComboBox;<br> ADOQuery1: TADOQuery;<br> Button4: TButton;<br> Edit2: TEdit;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> procedure ComboBox1KeyPress(Sender: TObject; var Key: Char);<br> procedure Button3Click(Sender: TObject);<br> procedure ComboBox1Change(Sender: TObject);<br> procedure Button4Click(Sender: TObject);<br> private<br> { Private declarations }<br> procedure TC;<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> s:string;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.TC;<br>var<br> i:integer;<br>begin<br> AD.First ;<br> for i:=1 to AD.RecordCount do<br> begin<br> AD.Edit ;<br> AD.FieldByName('ID').AsInteger :=i;<br> AD.Post ;<br> AD.Next ;<br> end;<br> AD.First ;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> i:integer;<br>begin<br> if trim(Edit1.Text)<>'' then<br> begin<br> i:=AD.RecordCount+1;<br> AD.AppendRecord([i,trim(Edit1.Text),ComboBox2.Text,trim(Edit2.Text)]);<br> Edit1.Text :='';<br> Edit2.Text :='';<br> Combobox1.Text :=Combobox1.Items.Strings[0];<br> Edit2.Visible :=True;<br> Edit1.SetFocus ;<br> end<br> else<br> begin<br> messagebox(0,'请输入!','系统信息',MB_ICONQUESTION+0);<br> end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> if AD.RecordCount<>0 then<br> begin<br> if messagebox(0,'是否要删除数据?','系统信息',MB_YESNO+MB_ICONQUESTION+MB_TASKMODAL)=IDYES then<br> begin<br> AD.Delete ;<br> TC;<br> end;<br> end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> ADOConnection1.Connected :=False;<br> ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+ExtractFilePath(Application.ExeName)+'Demo/Demo.mdb'+';Persist Security Info=False';<br> ADOConnection1.Connected :=True;<br><br> AD.Active :=True;<br>end;<br><br>procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);<br>begin<br> key:=#13;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br> i:integer;<br>begin<br> if AD.RecordCount<>0 then<br> begin<br> try<br> ADOQuery1.Active :=False;<br> ADOQuery1.SQL.Text:='Drop table B';<br> ADOQuery1.Active :=True;<br> except<br><br> end;<br><br> try<br> AD.First ;<br> s:='create table B(';<br> for i:=0 to AD.RecordCount-1 do<br> begin<br> if AD.FieldByName('PV').AsString<>'' then<br> begin<br> s:=s+AD.FieldByName('PL').AsString+' '+AD.FieldByName('PP').AsString+'('+AD.FieldByName('PV').AsString+')'+',';<br> end<br> else<br> begin<br> s:=s+AD.FieldByName('PL').AsString+' '+AD.FieldByName('PP').AsString+',';<br> end;<br> AD.Next ;<br> end;<br> Delete(s,Length(s),1);<br> s:=s+')';<br><br> ADOQuery1.Active :=False;<br> ADOQuery1.SQL.Text:=s;<br> ADOQuery1.Active :=True;<br> except<br><br> end;<br><br> ADOConnection1.Connected :=False;<br> ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+ExtractFilePath(Application.ExeName)+'Demo/Demo.mdb'+';Persist Security Info=False';<br> ADOConnection1.Connected :=True;<br><br> AD.Active :=True;<br> AC.Active :=True;<br> end;<br>end;<br><br>procedure TForm1.ComboBox1Change(Sender: TObject);<br>begin<br> combobox2.ItemIndex :=combobox1.ItemIndex ;<br> if Combobox1.Text='文本' then<br> begin<br> Edit2.Visible :=True;<br> end<br> else<br> begin<br> Edit2.Visible :=False;<br> end;<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>var<br> Engine:OleVariant;<br> db:OleVariant;<br> i:integer;<br>begin<br> if AD.RecordCount<>0 then<br> begin<br> try<br> ADOQuery1.Active :=False;<br> ADOQuery1.SQL.Text:='Drop table B';<br> ADOQuery1.Active :=True;<br> except<br><br> end;<br><br> Engine:=CreateOLEObject('DAO.DBEngine.36');<br> //Engine.CreateDatabase(ExtractFilePath(Application.ExeName)+'Demo/QQ.mdb',';LANGID=0x0804;CP=936;COUNTRY=0;',2);<br> db:=Engine.OpenDatabase(ExtractFilePath(Application.ExeName)+'Demo/Demo.mdb');<br> try<br> AD.First ;<br> s:='create table B(';<br> for i:=0 to AD.RecordCount-1 do<br> begin<br> if AD.FieldByName('PV').AsString<>'' then<br> begin<br> s:=s+AD.FieldByName('PL').AsString+' '+AD.FieldByName('PP').AsString+'('+AD.FieldByName('PV').AsString+')'+',';<br> end<br> else<br> begin<br> s:=s+AD.FieldByName('PL').AsString+' '+AD.FieldByName('PP').AsString+',';<br> end;<br> AD.Next ;<br> end;<br> Delete(s,Length(s),1);<br> s:=s+')';<br> db.Execute(s);<br> db.close;<br> except<br><br> end;<br><br> ADOConnection1.Connected :=False;<br> ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+ExtractFilePath(Application.ExeName)+'Demo/Demo.mdb'+';Persist Security Info=False';<br> ADOConnection1.Connected :=True;<br><br> AD.Active :=True;<br> AC.Active :=True;<br> end;<br>end;<br><br>{文本 text<br>备注 memo<br>数字 int<br>日期/时间 datetime<br>货币 money <br>自动编号 int identity (1, 1) primary key<br>是/否 bit<br>OLE对象 image<br>}<br><br>end.