在嵌入连接字符串中指定 mdw 文件到另一个 Jet mdb:可能吗?
我可以在 Jet (mdb) 查询中使用以下语法从另一个 .mdb 文件中选择数据:
SELECT * FROM [Database=C:\Tempo\AnotherDB.mdb;].MyTable
或者
SELECT * FROM MyTable IN 'C:\Tempo\Another.mdb'
我想扩展它以使用工作组安全性,即用户级安全性 (ULS)。 我知道如何指定用户 ID 和密码,例如,
SELECT *
FROM
[Database=C:\Tempo\AnotherDB.mdb;UID=Admin;PWD=***;].MyTable
但这仅在两个 mdb 共享相同的 mdw 时才有效。
如何指定用于保护其他 mdb 的 mdw 文件的路径? 是否可以? 如果没有,为什么不呢?
PS 我很多年前在 中问过这个问题此访问新闻组帖子但没有收到回复。 在 Access MVP 说服我这是不可能的之后,我放弃了; 我不记得细节了,但它与底层架构有关(工作区仅支持一个工作组文件,并且没有 Jet 查询实例化新工作区的机制?无论如何都是类似的东西)。
I can use the following syntax in a Jet (mdb) query to select data from another .mdb file:
SELECT * FROM [Database=C:\Tempo\AnotherDB.mdb;].MyTable
alternatively
SELECT * FROM MyTable IN 'C:\Tempo\Another.mdb'
I want to extend this to use workgroup security a.k.a. User Level Security (ULS). I know how to specify a userID and password e.g.
SELECT *
FROM
[Database=C:\Tempo\AnotherDB.mdb;UID=Admin;PWD=***;].MyTable
but this only works when both mdb's share the same mdw.
How would I specify the path of the mdw file used to secure the other mdb? Is it possible? If not, why not?
P.S. I asked this question many years ago in this Access newsgroup post but received no reply. I gave up after an Access MVP convinced me it was not possible; I don't recall the details but it was something to do with the underlying architecture (a workspace supports only one workgroup file and there is no mechanism for a Jet query to instantiate a new workspace? something like that anyway).
My renewed interest has been sparked by this SO comment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简短的回答是否定的。 如果您使用源数据库,则无法指定另一个工作组,如果将其留空并指定有效的连接字符串,则您将收到 ISAM 错误。 (尽管该方法对于 SQL Server 等其他数据库也适用。)
示例(不起作用):
但是您可以使用 ADO 来完成此操作。 我不确定您在查询后想要完成什么,但以下是如何将其放入记录集:
值得注意的是,这两种方法最终都会对帐户凭据进行硬编码。 最好是 A.) 提示用户提供所述信息,或者 B.) 使用只读访问权限限制在最低限度的特殊帐户。 还可能建议您采取一些步骤来混淆代码中的信息,并且保护项目的密码实际上不会以任何方式模糊存储在文件中的文本。
Short answer is no. If you use the source db then you can't specify another workgroup and if leave it blank and specify a valid connection string then you will get an ISAM error. (Although that method works just fine for other databases such as SQL Server.)
Example (does not work):
However you can do it with ADO. I'm not sure what you are trying to accomplish once you have the query, but here is how to get it into a recordset:
It is worth noting that both methods end up hard coding the account credentials. It might be better to either A.) Prompt the user to provide said info, or B.) Use a special account with read-only access limited to the minimum. You might also be well advised to take a few steps to obfuscate the info in your code as well as password protecting your project does not actually obscure the text stored in the file in any way.
简单的回答:这是不可能的。
Simple answer: it's not possible.
嗯...实际上您可以使用连接字符串。 它与 SQL Server 配合使用效果最佳。 我只是展示如果它与 Access 一起使用会是什么样子。 我试图说明的一点是,存在有效的语法...只是 Access 不允许您使用 Access 执行此操作。 (如果连接到其他类型的数据库,它将让您这样做。)有趣的是,该功能与其他类型的数据库兼容,而不是 Access...当它是 Access,但确实如此。
尽管如果它们使用相同的 MDW 进行保护,那么您所要做的就是这样,并且查询将在当前登录用户的上下文中运行:
Well... actually you can use the connection string. It works best with SQL Server. I was just showing how it would like if it worked with Access. The point I was trying to make is that there is a valid syntax... It's just that Access won't allow you to do that with Access. (It will let you do that if the connection is to another type of database.) Funny that the feature is compatible with other types of DBs and not Access... When it's Access, but true nevertheless.
Although if they are secured with the same MDW then all you have to do is this, and the query will run in the context of the currently logged in user:
也许我应该重新巩固和澄清:)在你原来的问题的背景下,答案是:
如果数据库是:都是 Access,并且具有不同的 mdw,则无法通过 SQL/查询生成器执行此操作。 但是,如果两个数据库都是 Access 并且具有相同 mdw(如您后面的评论所示),那么您可以通过 SQL/Query 完成此操作建造者。 在该特定场景中,您不会(不能)通过连接字符串执行此操作,您可以按照下面的步骤 1-4 执行此操作,然后将步骤 5a 替换为 5b。
如果数据库都是 Access 且具有不同 mdw,则指定不同工作组的唯一方法是使用连接字符串。 但是如果您尝试通过连接字符串属性(在下面的步骤 5a 中),SQL/查询生成器将引发 ISAM 错误。 因此您无法通过查询构建器来完成此操作。 您可以使用 ADO 通过 VBA 来完成此操作,如我之前发布的示例所示。
请记住,查询生成器中的连接字符串属性适用于非-Access 数据库,源数据库适用于具有相同(或无)mdw 的Access 数据库。 对于具有不同 mdw 的 Access 数据库,我所知道的唯一解决方案是我之前发布的 VBA 解决方案。
现在直接在 SQL 中使用连接字符串。 你可以做到这一点。 但有一些警告:它不能与其他 Access 数据库一起使用。 如果您使用 SQL Sever dbo 表执行此操作,则必须编辑查询构建器生成的 SQL 以删除“dbo”。 字首。
我认为最简单的方法就是引导您完成我想说的内容。
5a. 在“源连接字符串”中粘贴连接字符串。
*5b。 将另一个数据库的路径粘贴到“源数据库”中。
但这几乎是无关紧要的,因为这种方法虽然对非 Access 数据库有效,但不适用于 Access。 (我知道这有点令人沮丧。)
使用的连接字符串示例:
仅供参考,您使用连接字符串,如上所示,但如果您使用 Source Db 属性,您将得到以下内容:)(我在发布之前测试了所有示例:))
Maybe I should reconsolidate and clarify:) In the context of your original question, the answer is:
You can't do it via SQL/Query Builder, if the databases are: both Access, and have a different mdw. However, if both databases are Access and have the same mdw, (as your later comment indicated) then you can do it via SQL/Query Builder. In that specific scenario you would not (can't) do it via a connection string you would do it following the steps 1-4 below, then replace step 5a with 5b.
If the databases are both Access and have a different mdw, the only way to specify a different workgroup is with the connection string. However the SQL/Query Builder will throw a ISAM error if you try to via the connection string property (in step 5a below). So you can't do it via the Query builder. You can do it with VBA using ADO as in my example posted previously.
Remember that the connection string property in the query builder is for non-Access databases, source database is for Access databases with the same (or no) mdw. For Access databases with different mdws the only solution I am aware of is the VBA one I posted previously.
Now as to using the connection string directly in SQL. You can do this. But there are some caveats: It won't work with other Access databases. If you do it with SQL Sever dbo tables, you will have to edit the SQL the query builder generates to remove the "dbo." prefix.
I think the simplest way is to just walk you through what I am trying to say.
5a. In "Source Connect Str" paste the Connection String.
*5b. Paste the path to the other database in "Source Database".
But it's almost irrelevant as this approach, while valid for non-Access databases, does not work for Access. (It's a bit frustrating I know.)
Example of connection string in use:
Just as an FYI, you use the connection string, as shown above, but if you use the Source Db property, you will get what is below:) (I tested all my examples before I posted them:))
您可以在使用ACCESS时使用用户定义函数读取来自另一个工作组的数据。
对 Access 的限制是因为只有 Access 具有允许您使用用户定义函数的 VB 挂钩。 它相当于使用 SQL Server 用户定义的函数——仅当您有 SQL Server 时才运行。
对只读的限制是因为 Access 不提供对用户定义变量的挂钩,而只提供对用户定义函数的挂钩。
在 Access 中的用户定义函数中,您可以放置如下代码:
在该 UDF 中,您可以使用该工作区通过“任意”工作组文件从另一个数据库读取数据。
注意:如果您从选择查询中调用此函数,请将 dbe 和 ws 以及 db 和 rs 设为全局,这样您就不必在每次触摸记录中的字段时打开文件。 然后该函数只有
注意:如果您确实愿意,您可以将更新查询放入从 select 语句调用的 udf 中。 如果您希望稍后在 thedailywtf.com 上看到它,请这样做。
PS:这是一个不寻常的解决方案。 我大约在 2004 年,但你不可能通过发布到“入门”来得到这个
You can use a User Defined Function to READ data from another workgroup while using ACCESS.
The restriction to Access is because only Access has the VB hooks which allow you to use User Defined Functions. It is equivilant to using SQL server user defined functions -- which run only if you have SQL Server.
The restriction to read-only is because Access does not provide hooks to User Defined variables, only user defined functions.
In Access, in a User Defined Function, you put code like this:
In that UDF you can use that workspace to read data from from another database, using the "whatever" workgroup file.
Note: If you call this from a select query, make dbe and ws and db and rs global, so that you don't have open the files every time you touch a field in a record. Then the function just has
Note: If you really want to, you can put an update query into a udf called from a select statement. Do that if you hope to see it posted on thedailywtf.com at some later date.
PS: it's an unusual solution. I was around in 2004, but you wouldn't have got this by posting to "gettingstarted"