使用C#将一个Access数据库复制到另一个数据库 [英] Copy one access database into another database with C#

查看:809
本文介绍了使用C#将一个Access数据库复制到另一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式将一个数据库中的所有表复制到可能已经包含表的另一个数据库中(并且如果有重复的名称,当然会引发异常)?这意味着要在目标数据库中创建具有适当结构的表,并使用与源数据库相同的名称来创建表.

Is it possible to copy programmatically all the tables in one database into another database that might already contain tables (and if there is any repeated name throw an exception of course)? This implies creating the tables in the destination database with the proper structures and with the same name as on the source database.

我发现了一些类似的问题,但没有一个有这个特殊的需求.

I found a few similar questions but none of them have this particular need.

我正在使用Jet引擎.

I'm using the Jet engine.

推荐答案

我意识到这是一个老问题.也许这个答案会帮助别人.

I realize this is an old question. Perhaps this answer will help someone else.

操作Microsoft Access的COM接口似乎可以提供您所需要的大多数内容.

Manipulating the COM interface of Microsoft Access seems to provide most of what you are looking for.

  • 添加对"Microsoft Access xx.x对象库"的引用
  • 创建并实例化"Microsoft.Office.Interop.Access.Application"类型的对象
  • 使用参数为您的源数据库调用OpenCurrentDatabase
  • 使用目标数据库的参数,目标表的名称,对象类型(AcObjectType.acTable)和源表的名称调用DoCmd.CopyObject.
  • 调用CloseCurrentDatabase

在VB.Net中,是这样的:

In VB.Net, it's something like this:

acdb = New Microsoft.Office.Interop.Access.Application
acdb.OpenCurrentDatabase("source db")
acdb.DoCmd.CopyObject("target db", "target table name", Microsoft.Office.Interop.Access.AcObjectType.acTable, "source table name")
acdb.CloseCurrentDatabase()

我确认使用正确的列类型以及索引和键正确复制了表.但是,如果目标表已经存在而没有任何警告或异常,它似乎会覆盖目标表.

I verified the tables are copied correctly, with proper column types and indexes and keys. However, it seems to overwrite the target table if it already exists without any warnings or exceptions.

有关详细信息,请参见此MSDN 链接.

See this MSDN link for details.

在研究如何执行此操作时,由于我以为是DAO,所以我误解了所读内容.自从使用DAO以来已经很长时间了,所以我没有意识到自己的错误方式.谢谢Gord Thompson让我挺直.

When I researching how to do this, I misunderstood what I read, as I thought this was DAO. It's been a very long time since I've used DAO, so I didn't realize the errors in my ways. Thank you Gord Thompson for setting me straight.

这确实需要在运行它的计算机上安装MSACCESS.更复杂的方法是使用ADOX,它也有其缺点IMHO.

This does require MSACCESS being installed on the machine this is running on. The more complicated approach would be to use ADOX, which also has its own drawbacks IMHO.

这是与Office 2010一起使用的.显然,YMMV与其他版本一起使用.

This is with Office 2010. Obviously YMMV with other versions.

如果只有一天,Microsoft告诉世界其他地方,他们开始使用.Net for Office.

If only some day Microsoft started using .Net for Office as they tell the rest of the world to do.

这篇关于使用C#将一个Access数据库复制到另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆