Use C # To dynamically create an Access database

Source: Internet
Author: User
Remember that the mdb files used to dynamically create the Access database were DAO, which was developed with VC and had a lot of APIS, which was very troublesome. Some people seem to mention DAO now. In fact, the simplest way to dynamically create mdb data is ADOX. The method for creating an access database with ADOX is simple. You only need to Create a new Catalog object and then call its Create

Remember that the mdb files used to dynamically create the Access database were DAO, which was developed with VC and had a lot of APIS, which was very troublesome. Some people seem to mention DAO now. In fact, the simplest way to dynamically create mdb data is ADOX. The method for creating an access database with ADOX is simple. You only need to Create a new Catalog object and then call its Create

Remember that the mdb files used to dynamically create the Access database were DAO, which was developed with VC and had a lot of APIS, which was very troublesome. Some people seem to mention DAO now. In fact, the simplest way to dynamically create mdb data is ADOX.

The method for creating an access database with ADOX is simple. You only need to Create a new catox object and then call its Create method, as shown below:
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. Next, I will introduce the implementation details in c. First, you need to Add references. In the "Add reference" dialog box, switch to the Com page, select "Microsoft ADO Ext. 2.8 for DDL and Security", and click "OK. The using ADOX namespace at the beginning of the file. Then, add the code shown above to successfully create the Access database. The Code is as follows:

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 useless. We need to create a table. Before creating a table, we must connect to the target database. The bridge used to connect data is actually the Connection object of ADO, so we have to add the application to ADO again, in the Add reference dialog box, switch to the Com page, select "Microsoft ActiveX Data Objects 2.8 Library", and click OK. The complete code for creating a table is as follows:

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, four fields are added to the table, and a primary key is set. The fields in the table are input to different common types in 4. The first field is an Automatically increasing Integer type. This type is special. You must set the ParentCatalog attribute for this field, set the property value of "AutoIncrement" to true .. The Text type in Access corresponds to adVarWchar, And the date type corresponds to adDate.

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.