vba ADO 和 DAO 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9737099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between ADO and DAO
提问by APrough
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.
这不是关于哪个更好的问题,而是关于为什么它们在功能上不同的问题。我遇到的问题已经解决,但我很好奇为什么会发生这种行为。
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.
背景 - 使用 Excel vba 从 Access 数据库中提取数据。当用户单击按钮时,会从 Access 中提取一个记录集,并将各种数据填充到电子表格中。然后,从不同的查询中提取另一个记录集以填充电子表格的另一部分。
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).
ADO 的作用 - ADO 非常适合我的第一个记录集。但是,我的第二个记录集转到 Access 中的查询,运行,并且不返回任何行。如果我在 Access 中运行此查询,它会打开(大约 3 到 4 秒后)。这个查询有多个连接、计算项、限制和可能的联合查询(我尝试了很多不同的方式,有/没有联合等)。我尝试关闭并重新打开 ado 连接。我尝试更改超时值,我什至测试使用 ADO 命令来运行针对此数据的 Make table 查询,然后从表中提取(这顺便说一下,但不是最好的情况,因为数据不断变化,而且我不想每次有人使用这个工具时都必须运行 make table 查询)。
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.
因此,我将第二个数据拉取更改为 DAO,您瞧,它起作用了。第一个数据拉取仍然是 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.
那么,有人可以向我解释为什么 ADO 不会在一种情况下提取数据,而 DAO 会吗?同样,这纯粹是为了提供信息。
采纳答案by Matt Donnan
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.
DAO 是 Jet (Ms-Access) 数据表的本机数据访问方法。ADO“Active X 数据对象”是与几乎所有类型数据库的行业友好连接。
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.
对于标准查询,在这种情况下没有理由为什么 ADO 不应该在 DAO 返回的地方返回任何记录,我怀疑查询还必须包含与 Access 数据库中的项目相关的参数。如果是这种情况,那么 ADO 将不起作用,因为它没有使用所述参数的选项,因为它只是对 Excel 的外部引用,使用 DAO 方法将触发 Access 运行查询而不是 Excel,因此它将能够访问它自己的参数/引用。
回答by HK1
I think this question was answered very well already but I want to add another excellent (although somewhat hard to find) resource for comparing DAO and ADO. It's targeted primarily at Access applications with Access forms and reports, but much of the information here would apply to a solution written in Excel.
我认为这个问题已经得到很好的回答,但我想添加另一个优秀的(虽然有点难找到)资源来比较 DAO 和 ADO。它主要针对带有 Access 表单和报表的 Access 应用程序,但这里的大部分信息都适用于用 Excel 编写的解决方案。
回答by Purushottam Swami
In Visual Basic, three data access interfaces are available to you: ActiveX Data Objects (ADO), Remote Data Objects (RDO), and Data Access Objects (DAO). A data access interface is an object model that represents various facets of accessing data. Using Visual Basic, you can programmatically control the connection, statement builders, and returned data for use in any application.
在 Visual Basic 中,您可以使用三个数据访问接口:ActiveX 数据对象 (ADO)、远程数据对象 (RDO) 和数据访问对象 (DAO)。数据访问接口是表示访问数据的各个方面的对象模型。使用 Visual Basic,您可以以编程方式控制连接、语句生成器和返回的数据,以便在任何应用程序中使用。
Why are there three data access interfaces in Visual Basic? Data access technology is constantly evolving, and each of the three interfaces represent a different state of the art. The latest is ADO, which features a simpler — yet more flexible — object model than either RDO or DAO. For new projects, you should use ADO as your data access interface.
为什么 Visual Basic 中有三个数据访问接口?数据访问技术在不断发展,三个接口中的每一个都代表了不同的技术状态。最新的是 ADO,它具有比 RDO 或 DAO 更简单但更灵活的对象模型。对于新项目,您应该使用 ADO 作为您的数据访问接口。