ADO.NET 创建链接表的方法
我正在编写一个使用 ADO.NET OLEDB 提供程序的应用程序。数据库是Access。大多数数据库交互是通过 DDL/DML SQL 查询。
我现在需要创建链接表,但似乎没有办法单独使用 ADO.NET 来做到这一点。既不是简单的 DDL 查询,也不是尝试直接操作 Access 系统表。
我试图避免使用 ADOX,并在我的应用程序中使用额外的引用/依赖项。有人知道解决这个问题的方法吗?非常感谢。
以下是我目前使用 ADOX 创建链接表的方法。
using ADOX;
public static void CreateLinkedTable(string sourceDB, string sourceTable, string targetDB, string targetTable)
{
Catalog cat = new Catalog();
cat.let_ActiveConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + targetDB);
Table table = new Table();
table.Name = targetTable;
table.let_ParentCatalog(cat);
table.Properties["Jet OLEDB:Create Link"].Value = true;
table.Properties["Jet OLEDB:Link Datasource"].Value = sourceDB;
table.Properties["Jet OLEDB:Remote Table Name"].Value = sourceTable;
cat.Tables.Append(table);
}
I'm writing an application that uses ADO.NET OLEDB provider. Database is Access. Most of DB interaction is through DDL/DML SQL queries.
I now need to create linked tables and there doesn't seem to be a way of doing that with ADO.NET alone. Neither in a simple DDL query, nor with trying to manipulate Access system tables directly.
I'm trying to avoid using ADOX, with the extra reference/dependency in my application. Anyone knows a way around this? Much appreciated.
Here's how I currently create linked tables with ADOX.
using ADOX;
public static void CreateLinkedTable(string sourceDB, string sourceTable, string targetDB, string targetTable)
{
Catalog cat = new Catalog();
cat.let_ActiveConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + targetDB);
Table table = new Table();
table.Name = targetTable;
table.let_ParentCatalog(cat);
table.Properties["Jet OLEDB:Create Link"].Value = true;
table.Properties["Jet OLEDB:Link Datasource"].Value = sourceDB;
table.Properties["Jet OLEDB:Remote Table Name"].Value = sourceTable;
cat.Tables.Append(table);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有办法解决额外的依赖性。链接表是 MS ACCESS 的专有功能,而 ADO.NET 不知道如何执行您希望它执行的操作。 Access 必须通过它接受的 SQL 命令来公开这一点,但事实并非如此,它是通过 VBA 公开的。因此,您可以从 Access DB 内部控制它,但不能从外部 - 从 ADO.NET 连接,您所能做的就是发出它知道如何解释的 SQL 语句。
There's not a way around the extra dependency. Linked Tables are a propertiary feature of MS ACCESS, and ADO.NET doesn't know how to do what you want it to do. Access would have to expose this through the SQL commands it accepts, and it doesn't, it's exposed to the extent that it is, through VBA. So you can control it from within the Access DB, but not from outside -- from a ADO.NET connection, all you can do is issue SQL statements that it know how to interpret.