Create an Access database dynamically with C #

Source: Internet
Author: User
Tags reference access database
access| Create | news | data | database

Remember the previous to dynamic create an Access database MDB files are used DAO, VC development, a lot of API, is very troublesome. It seems that there is also a little mention of DAO. In fact, the simplest way to create an MDB data dynamically is to ADOX.
Using ADOX to create an Access database method is simple, you only need a new catalog object, and then call its Create method, as follows:

ADOX. Catalog Catalog = new Catalog ();
Catalog. Create ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb; Jet oledb:engine type=5 ");
Just two lines of code are done. Down I mainly introduce the implementation details in C #. First you want to add a reference, switch to the COM page in the "Add Reference" dialog box, select "Microsoft ADO Ext. 2.8 for DDL and security" and click OK. Using ADOX namespace at the beginning of the file. You can then add the code as shown above to successfully create an Access database with the following code:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using ADOX;

namespace testadox
... {
    class program
    ... {
        static void Main (string[] args)
         ... {
            ADOX. Catalog Catalog = new Catalog ();
            Catalog. Create ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb; Jet oledb:engine type=5 ");
       }
   }
}
Creating a database file is of no practical use, and we want to create a table. Before creating the table, we have to connect to the target database, the bridge that connects the data is actually the connection object of ADO, so we have to add the application to ADO again, switch to COM page in the Add Reference dialog box, select "Microsoft ActiveX Data Objects 2.8 Library ", then click OK. Below is the complete code to create the table: using System;
using System.Collections.Generic;
using System.Text;
using ADOX;

namespace testadox
... {
    class program
    ... {
        static void Main (string[] args)
         ... {
            ADOX. Catalog Catalog = new Catalog ();
            Catalog. Create ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb; Jet oledb:engine type=5 ");

ADODB. Connection cn = New ADODB. Connection ();

cn. Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb", NULL, NULL,-1);
Catalog. ActiveConnection = cn;

ADOX. Table table = new ADOX. Table ();
Table. Name = "Firsttable";

ADOX. Column column = new ADOX. Column ();
Column. parentcatalog = Catalog;
Column. Name = "RecordID";
Column. Type = Datatypeenum.adinteger;
Column. DefinedSize = 9;
Column. properties["AutoIncrement"]. Value = true;
Table. Columns.Append (column, Datatypeenum.adinteger, 9);
Table. Keys.append ("Firsttableprimarykey", keytypeenum.adkeyprimary, column, NULL, NULL);
Table. Columns.Append ("CustomerName", Datatypeenum.advarwchar, 50);
Table. Columns.Append ("Age", Datatypeenum.adinteger, 9);
Table. Columns.Append ("Birthday", datatypeenum.addate, 0);
Catalog. Tables.append (table);

cn. Close ();
}
}
}
In the code above, a table named Firsttable is created, 4 fields are added to the list, and a primary key is set. The fields in the table enter 4 different common types, the first field is an automatically growing integer type, this type is special, you have to set the ParentCatalog property for this field and set the "AutoIncrement" property value to True ... The text type in Access corresponds to the adVarWChar, and the date type corresponds to the addate.
The key is set as table. Keys.append ("Firsttableprimarykey", keytypeenum.adkeyprimary, column, NULL, NULL), if the foreign key, you also have to set the associated table and associated fields, The latter two fields of the Append method.
You can also create indexes and views by referencing the code above.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.