回赠大富翁论坛对我的水平提高,现在特意发布<动态创建Access数据库>的完整代码 ( 积分: 0 )

  • 主题发起人 主题发起人 菜鸟黄
  • 开始时间 开始时间

菜鸟黄

Unregistered / Unconfirmed
GUEST, unregistred user!
回赠大富翁论坛对我的水平提高,现在特意发布&lt;动态创建Access数据库&gt;的完整代码<br><br>unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;DB,&nbsp;ADODB,&nbsp;StdCtrls,&nbsp;Grids,&nbsp;DBGrids,&nbsp;ExtCtrls,&nbsp;DBCtrls,<br>&nbsp;&nbsp;ComObj,&nbsp;dbtables;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;DataSource1:&nbsp;TDataSource;<br>&nbsp;&nbsp;&nbsp;&nbsp;AD:&nbsp;TADOQuery;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1:&nbsp;TADOConnection;<br>&nbsp;&nbsp;&nbsp;&nbsp;Panel1:&nbsp;TPanel;<br>&nbsp;&nbsp;&nbsp;&nbsp;DBGrid1:&nbsp;TDBGrid;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit1:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button2:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Panel2:&nbsp;TPanel;<br>&nbsp;&nbsp;&nbsp;&nbsp;DBGrid2:&nbsp;TDBGrid;<br>&nbsp;&nbsp;&nbsp;&nbsp;Panel3:&nbsp;TPanel;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button3:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Label1:&nbsp;TLabel;<br>&nbsp;&nbsp;&nbsp;&nbsp;Label2:&nbsp;TLabel;<br>&nbsp;&nbsp;&nbsp;&nbsp;ComboBox1:&nbsp;TComboBox;<br>&nbsp;&nbsp;&nbsp;&nbsp;DataSource2:&nbsp;TDataSource;<br>&nbsp;&nbsp;&nbsp;&nbsp;AC:&nbsp;TADOQuery;<br>&nbsp;&nbsp;&nbsp;&nbsp;DBNavigator1:&nbsp;TDBNavigator;<br>&nbsp;&nbsp;&nbsp;&nbsp;ComboBox2:&nbsp;TComboBox;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1:&nbsp;TADOQuery;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button4:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit2:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button2Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;FormCreate(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;ComboBox1KeyPress(Sender:&nbsp;TObject;&nbsp;var&nbsp;Key:&nbsp;Char);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button3Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;ComboBox1Change(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button4Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;TC;<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br>&nbsp;&nbsp;s:string;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TForm1.TC;<br>var<br>&nbsp;&nbsp;i:integer;<br>begin<br>&nbsp;&nbsp;AD.First&nbsp;;<br>&nbsp;&nbsp;for&nbsp;i:=1&nbsp;to&nbsp;AD.RecordCount&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;AD.Edit&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;AD.FieldByName('ID').AsInteger&nbsp;:=i;<br>&nbsp;&nbsp;&nbsp;&nbsp;AD.Post&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;AD.Next&nbsp;;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;AD.First&nbsp;;<br>end;<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;i:integer;<br>begin<br>&nbsp;&nbsp;if&nbsp;trim(Edit1.Text)&lt;&gt;''&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;i:=AD.RecordCount+1;<br>&nbsp;&nbsp;&nbsp;&nbsp;AD.AppendRecord([i,trim(Edit1.Text),ComboBox2.Text,trim(Edit2.Text)]);<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit1.Text&nbsp;:='';<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit2.Text&nbsp;:='';<br>&nbsp;&nbsp;&nbsp;&nbsp;Combobox1.Text&nbsp;:=Combobox1.Items.Strings[0];<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit2.Visible&nbsp;:=True;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit1.SetFocus&nbsp;;<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;messagebox(0,'请输入!','系统信息',MB_ICONQUESTION+0);<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.Button2Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;if&nbsp;AD.RecordCount&lt;&gt;0&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;messagebox(0,'是否要删除数据?','系统信息',MB_YESNO+MB_ICONQUESTION+MB_TASKMODAL)=IDYES&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AD.Delete&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TC;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.FormCreate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;ADOConnection1.Connected&nbsp;:=False;<br>&nbsp;&nbsp;ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data&nbsp;Source='+ExtractFilePath(Application.ExeName)+'Demo/Demo.mdb'+';Persist&nbsp;Security&nbsp;Info=False';<br>&nbsp;&nbsp;ADOConnection1.Connected&nbsp;:=True;<br><br>&nbsp;&nbsp;AD.Active&nbsp;:=True;<br>end;<br><br>procedure&nbsp;TForm1.ComboBox1KeyPress(Sender:&nbsp;TObject;&nbsp;var&nbsp;Key:&nbsp;Char);<br>begin<br>&nbsp;&nbsp;key:=#13;<br>end;<br><br>procedure&nbsp;TForm1.Button3Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;i:integer;<br>begin<br>&nbsp;&nbsp;if&nbsp;AD.RecordCount&lt;&gt;0&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Active&nbsp;:=False;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.SQL.Text:='Drop&nbsp;table&nbsp;B';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Active&nbsp;:=True;<br>&nbsp;&nbsp;&nbsp;&nbsp;except<br><br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AD.First&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s:='create&nbsp;table&nbsp;B(';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i:=0&nbsp;to&nbsp;AD.RecordCount-1&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;AD.FieldByName('PV').AsString&lt;&gt;''&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s:=s+AD.FieldByName('PL').AsString+'&nbsp;'+AD.FieldByName('PP').AsString+'('+AD.FieldByName('PV').AsString+')'+',';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s:=s+AD.FieldByName('PL').AsString+'&nbsp;'+AD.FieldByName('PP').AsString+',';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AD.Next&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Delete(s,Length(s),1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s:=s+')';<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Active&nbsp;:=False;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.SQL.Text:=s;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Active&nbsp;:=True;<br>&nbsp;&nbsp;&nbsp;&nbsp;except<br><br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.Connected&nbsp;:=False;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data&nbsp;Source='+ExtractFilePath(Application.ExeName)+'Demo/Demo.mdb'+';Persist&nbsp;Security&nbsp;Info=False';<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.Connected&nbsp;:=True;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;AD.Active&nbsp;:=True;<br>&nbsp;&nbsp;&nbsp;&nbsp;AC.Active&nbsp;:=True;<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.ComboBox1Change(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;combobox2.ItemIndex&nbsp;:=combobox1.ItemIndex&nbsp;;<br>&nbsp;&nbsp;if&nbsp;Combobox1.Text='文本'&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit2.Visible&nbsp;:=True;<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit2.Visible&nbsp;:=False;<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.Button4Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;Engine:OleVariant;<br>&nbsp;&nbsp;db:OleVariant;<br>&nbsp;&nbsp;i:integer;<br>begin<br>&nbsp;&nbsp;if&nbsp;AD.RecordCount&lt;&gt;0&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Active&nbsp;:=False;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.SQL.Text:='Drop&nbsp;table&nbsp;B';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Active&nbsp;:=True;<br>&nbsp;&nbsp;&nbsp;&nbsp;except<br><br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Engine:=CreateOLEObject('DAO.DBEngine.36');<br>&nbsp;&nbsp;&nbsp;&nbsp;//Engine.CreateDatabase(ExtractFilePath(Application.ExeName)+'Demo/QQ.mdb',';LANGID=0x0804;CP=936;COUNTRY=0;',2);<br>&nbsp;&nbsp;&nbsp;&nbsp;db:=Engine.OpenDatabase(ExtractFilePath(Application.ExeName)+'Demo/Demo.mdb');<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AD.First&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s:='create&nbsp;table&nbsp;B(';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i:=0&nbsp;to&nbsp;AD.RecordCount-1&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;AD.FieldByName('PV').AsString&lt;&gt;''&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s:=s+AD.FieldByName('PL').AsString+'&nbsp;'+AD.FieldByName('PP').AsString+'('+AD.FieldByName('PV').AsString+')'+',';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s:=s+AD.FieldByName('PL').AsString+'&nbsp;'+AD.FieldByName('PP').AsString+',';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AD.Next&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Delete(s,Length(s),1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s:=s+')';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.Execute(s);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.close;<br>&nbsp;&nbsp;&nbsp;&nbsp;except<br><br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.Connected&nbsp;:=False;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data&nbsp;Source='+ExtractFilePath(Application.ExeName)+'Demo/Demo.mdb'+';Persist&nbsp;Security&nbsp;Info=False';<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.Connected&nbsp;:=True;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;AD.Active&nbsp;:=True;<br>&nbsp;&nbsp;&nbsp;&nbsp;AC.Active&nbsp;:=True;<br>&nbsp;&nbsp;end;<br>end;<br><br>{文本&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text<br>备注&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memo<br>数字&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int<br>日期/时间&nbsp;&nbsp;datetime<br>货币&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;money&nbsp;<br>自动编号&nbsp;&nbsp;&nbsp;int&nbsp;identity&nbsp;(1,&nbsp;1)&nbsp;primary&nbsp;key<br>是/否&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bit<br>OLE对象&nbsp;&nbsp;&nbsp;&nbsp;image<br>}<br><br>end.
 
上面写了两种创建的方法,供大家选择
 
如果大家都像楼主一样就好啦。<br>学习一下!
 
谢谢楼主,支持!!!~~~~~~~~~~~~~~~~~~
 
是,谢谢楼主,有好东西共享啊
 
精神是好的!不过东西太过时,<br>搞点技术含量高点的东西啥;<br>期待中.....
 
呵呵~是没有什么技术含量,只是给一些初学者一些方法而已,我也是个过来人.
 
是,谢谢楼主,有好东西共享啊
 
是,谢谢楼主,有好东西共享啊
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部