sammy sammy - 17 days ago 5
C# Question

Copy Access table structure and constraints using C# or SQL

Can someone tell me how to copy an MS Access table's table structure and all its constraints, keys, and etc. using .NET or SQL? I have searched and found a number of ways of copying the columns and types, but I need to bring across the constraints as well such as primary keys.

I have a database with a bunch of tables which I need to create copies of (structure only) with new names. This needs to be executed from within a C# application, but I do not care if it is done using ADO or SQL, just as long as I can find a way. Any tips would be greatly appreciated.

Answer

You can "export" to the same database, which will create a copy with all indexes etc, but not relationships.

        // Start a new instance of Access for Automation
        oAccess = new Access.Application();

        // Open a database
        oAccess.OpenCurrentDatabase(@"z:\docs\test.accdb");
        oAccess.DoCmd.TransferDatabase(
            Access.AcDataTransferType.acExport,
            "Microsoft Access",
            @"z:\docs\test.accdb",
            Access.AcObjectType.acTable, 
            "table1",
            "newtable",true,false);