哪位兄台能帮我把这段VB代码(不长)转为delphi中能运行的代码?通过则给分,不甚感谢!

chenjiong 2002-04-09 08:48:59
说明:不要看着长,其实大部分是些说明,我试着弄了弄,结果运行出错。这是一段关于用adox在程序运行时建立access链接表的代码,据说是可以进行的。对两个以上mdb库进行操作,无疑是个好办法。谢谢!来得及的话请写点注释。



总的来说是用adox,但要注意先import ado ext for ddl and security
李维那本讲ado的书上有介绍,把下面的vb代码改成delphi就是了

HOWTO: Link and Refresh Linked Jet Tables Using ADOX

--------------------------------------------------------------------------------
The information in this article applies to:

ActiveX Data Objects (ADO), versions 2.1 , 2.1 SP1 , 2.1 SP2 , 2.5 , 2.6 , 2.7

--------------------------------------------------------------------------------


SUMMARY
You can update the connection information for a linked table by using the
Data Access Objects (DAO) RefreshLink method. This article describes another
way to do this with ActiveX Data Objects (ADO) version 2.1 and later by using
the Tables collection exposed by the ADO Extensibility model (ADOX).



MORE INFORMATION

In Microsoft Visual Basic, create a new Standard EXE project. Form1 is added
to the project by default.


On the Project menu, click References . From the list of available components,
select Microsoft ActiveX Data Objects 2.1 Library and Microsoft ADO Ext. 2.1
for DDL and Security.


Place two CommandButton controls on Form1: cmdRefreshLink and cmdCreateLinkedTable.


Paste the following code in the Declarations section of Form1:


Option Explicit

Dim cn As ADODB.Connection

Private Sub Form_Load()
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Db1.mdb"
End Sub

Private Sub cmdCreateLinkedTable_Click()
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table

Set cat = New ADOX.Catalog
Set tbl = New ADOX.Table

' Open the catalog.
cat.ActiveConnection = cn

' Create the new Table.
tbl.Name = "Linked_Employees"
Set tbl.ParentCatalog = cat

' Set the properties to create the link.
tbl.Properties("Jet OLEDB:Link Datasource") = "D:\nwind.mdb"
tbl.Properties("Jet OLEDB:Remote Table Name") = "Employees"
tbl.Properties("Jet OLEDB:Create Link") = True

' Append the table to the Tables collection.
cat.Tables.Append tbl
Set cat = Nothing
End Sub

Private Sub cmdRefreshLink_Click()
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table

Set cat = New ADOX.Catalog
Set tbl = New ADOX.Table

' Open the catalog.
cat.ActiveConnection = cn
For Each tbl In cat.Tables
If tbl.Type = "LINK" And tbl.Name = "Linked_Employees" Then
tbl.Properties("Jet OLEDB:Link Datasource") = "D:\OtherSource.mdb"
End If
Next
End Sub
Modify the code in the Form_Load() event to point the connection string to a valid Database in the cn.Open method.


Modify the code in the cmdCreateLinkedTable_Click() event to point the connection string to the Northwind sample database in the tbl.Properties("Jet OLEDB:Link Datasource") assignment.


Modify the code in the cmdRefreshLink_Click() event to point the tbl.Properties("Jet OLEDB:Link Datasource") assignment to another valid location.


Run the project.


The cmdCreateLinkedTable() procedure creates a linked table, Linked_Employees, from the Northwind sample database Employees table. The cmdRefreshLink() procedure refreshes the linked tables with a new database location.



REFERENCES

Q240222 HOWTO: Use ADO to Refresh/Create Linked Table for Password Secured Jet 4.0 Database
"Defining and Retrieving a Database's Schema"
http://msdn.microsoft.com/library/techart/daotoadoupdate.htm






...全文
37 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenjiong 2002-04-11
  • 打赏
  • 举报
回复
算了。还是自己弄弄得了,版主帮我把帖子删了,谢谢!
befree 2002-04-11
  • 打赏
  • 举报
回复
var
cat:olevariant;
tdb:olevariant;
begin
d.Connected:=true;
cat:=createoleobject('adox.catalog');
cat.activeconnection:=d.ConnectionObject;
tdb:=createoleobject('adox.table');
tdb.name:='ware_zzz';
tdb.ParentCatalog:=cat;
tdb.Properties('Jet OLEDB:Exclusive Link'):=true;
tdb.Properties('jet oledb:link datasource'):='c:\x.mdb';
tdb.Properties('Jet OLEDB:Remote Table Name'):='ware';
tdb.Properties('Jet OLEDB:Create Link'):=True;
cat.Tables.Append(tdb);
befree 2002-04-11
  • 打赏
  • 举报
回复
我来了
哈哈
chenjiong 2002-04-10
  • 打赏
  • 举报
回复
是不长的,其实有很多是说明.
wangda 2002-04-10
  • 打赏
  • 举报
回复
这叫不长????
chenjiong 2002-04-10
  • 打赏
  • 举报
回复
genphone_ru(票票) :
不是用delphi访问mdb的问题,是建立mdb链接表的问题,那样可以用一个adocon....同时去操作几个mdb,多库操作时有用的。上面的VB代码就解决这问题,不过得转成delphi下的。
票票飞扬 2002-04-10
  • 打赏
  • 举报
回复
DBStr := 'F:\省委信息报送\MailDB.mdb';

ADOConnection.Connected := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+DBStr+';Persist Security Info=False';
ADOConnection.Connected := True;
milpas 2002-04-09
  • 打赏
  • 举报
回复
Delphi用ADO访问MDB很容易啊,有必要看VB的代码吗?
outer2000 2002-04-09
  • 打赏
  • 举报
回复
直接在DELPHI中导入ADOX,就可以使用了,很好用的。
chenjiong 2002-04-09
  • 打赏
  • 举报
回复
如何直接在DELPHI中导入ADOX?具体如何做?不是用delphi访问mdb的问题,是建立mdb链接表的问题,那样可以用一个adocon....同时去操作几个mdb,多库操作时有用的。上面的VB代码就解决这问题,不过得转成delphi下的。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧