ADO 和 DAO 的区别

问题描述:

这不是关于哪个更好的问题,而是关于为什么它们在功能上不同的问题.我遇到的问题已经解决了,但我很好奇为什么会发生这种行为.

This is not a question about which is better, but rather a question regarding why they differ functionally. The problem I was running into has been handled, but I am curious as to why this behavior is happening.

背景 - 使用 Excel vba 从 Access 数据库中提取数据.当用户单击按钮时,会从 Access 中提取一个记录集,并将各种数据填充到电子表格中.然后,从不同的查询中提取另一个记录集以填充电子表格的另一部分.

Background - using Excel vba to pull data from an Access database. When user clicks a button, a recordset is pulled from Access, and it populates various data to the spreadsheet. Then, another recordset is pulled from a different query to populate another part of the spreadsheet.

ADO 的作用 - ADO 非常适合我的第一个记录集.但是,我的第二个记录集转到 Access 中的查询,运行,并且不返回任何行.如果我在 Access 中运行此查询,它会打开(大约 3 到 4 秒后).这个查询有多个连接、计算项、限制和可能的联合查询(我尝试了很多不同的方式,有/没有联合等).我尝试关闭并重新打开 ado 连接.我尝试更改超时值,我什至使用 ADO 命令对此数据运行 Make table 查询进行测试,然后从表中拉取(顺便说一下,但这不是最好的情况,因为数据不断变化,而且我不想每次有人使用这个工具时都必须运行 make table 查询).

What ADO does - ADO works great for my first recordset. However, my second recordset goes to the query in Access, runs, and returns no rows. If I run this query in Access, it does open up (after about 3 to 4 seconds). This query has multiple joins, computed items, limits, and possibly Union queries (I tried it many different ways, with/without union,etc.). I tried closing and reopening the ado connection. I tried changing timeout values, and I even tested using an ADO command to run Make table queries for this data, and then pull from the table instead (this worked by the way, but is not the best-case, since the data changes continually, and I do not want to have to run the make table query everytime someone uses this tool).

因此,我将第二个数据拉取更改为 DAO,您瞧,它起作用了.第一个数据拉取仍然是ADO(我通常更喜欢使用),但现在正在考虑将其更改为DAO,因为我更希望代码中有一个数据访问方法.

So, I changed the second data pull to DAO, and lo and behold, it works. The first data pull is still ADO (which I generally prefer to use), but am now considering changing it to DAO, because I would rather have one data access method in the code.

那么,有人可以向我解释为什么 ADO 不会在一种情况下提取数据,而 DAO 会吗?同样,这纯粹是为了提供信息.

So, can someone explain to me why ADO will not pull the data in one case, but DAO will? Again, this is purely for informational purposes.

DAO 是 Jet (Ms-Access) 数据表的本机数据访问方法.ADO "Active X Data Objects" 是与几乎所有类型数据库的行业友好连接.

DAO is the native data access method for the Jet (Ms-Access) data tables. ADO "Active X Data Objects" is an industry friendly connection to almost all types of database.

对于标准查询,在这种情况下,ADO 没有理由不返回 DAO 返回的记录,我怀疑查询还必须包含与 Access 数据库中的项目相关的参数.如果是这种情况,则 ADO 将不起作用,因为它没有使用所述参数的选项,因为它只是对 Excel 的外部引用,使用 DAO 方法将触发 Access 运行查询而不是 Excel,因此它将能够访问它自己的参数/引用.

With a standard query there is no reason in this case why ADO should return no records where DAO does, I suspect it's that the query must also contain parameters relating to items within the Access database. If this is the case then ADO will not work as it will not have the option of using said parameters as it is just an external reference to Excel, using the DAO method will trigger Access to run the query rather than Excel and as such it will be able to access it's own parameters/references.