1

I'm trying to copy certain Access tables from one database to another in my C# winforms app.

The solution presented here works, but has the flaw that column properties aren't copied. For example, I have an Access table in which one string column has Allow Zero Length set to false. When the table is copied, the structure and data all come across, but Allow Zero Length becomes true. And many similar instances like this for many columns of many tables.

This is the approach I have been using:

SELECT c.*
INTO Contacts 
FROM Contacts AS c
IN 'C:\MyDB.mdb'

It works fine, but I only get structure and data, not the column properties.

I am already using this DAO dll for some other things, and would be happy to use it for this if I knew how:

enter image description here

This TransferDatabase() doc looks very promising, but I can't figure out how to get an instance/handle of the magical DoCmd. They never mention how to create that object, it just seems to always exist. I have added the reference to the dll and I have this Using statement as well:

using Microsoft.Office.Interop.Access.Dao;

But I can't find any way to create the DoCmd object, nor find the TransferDatabase() function.

So in summary, what's the best way to copy an Access table from one DB to another, including all structure, data, constraints, properties, indexes, etc, etc...everything...within a c# winforms application? Thanks in advance.

Community
  • 1
  • 1
HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
  • Please check this post: http://stackoverflow.com/questions/708042/ms-access-interop-data-import?rq=1 and also this one https://support.microsoft.com/en-us/kb/317114. The page you refer is for VBA, you need more code to call it in C# – Prisoner Sep 09 '16 at 02:15
  • Thanks Alex but none of those approaches seem to work with the Microsoft.Office.Interop.Access.Dao dll mentioned above. That is the dll referenced in our solution and the one I have access to. Is there some way to accomplish it with that dll? – HerrimanCoder Sep 09 '16 at 15:09
  • I don't have the `Access` library on hand, maybe you can try to `using Microsoft.Office.Interop.Access;`, which is the `Access` application itself, but `Dao` is the library to access the database object. After you can create the `Access.Application` instance, you may follow this https://support.microsoft.com/en-us/kb/317114 to call the `DoCmd` – Prisoner Sep 12 '16 at 01:20
  • Check approach mentioned in this post https://stackoverflow.com/questions/5482787/copy-one-access-database-into-another-database-with-c-sharp?rq=1 – kumar chandraketu Aug 01 '17 at 20:35

0 Answers0