How to create access database files in C #

Source: Internet
Author: User
How to create access database files in C #
Microsoft ADO ext.2.8
I want to use C # To create an Access database file (A. mdb ). How can this problem be achieved.
A. mdb files are not originally available, Program Create one and write data to it!

1.
※Create a project
※Choose solution> reference> Add reference.
Select Microsoft ADO ext.2.8 .....
-> Select-> OK
※Code
// Command Line Project Code As follows:
Using system;
Using ADOX;
Namespace consoleapplication1
{
Class class1
{
[Stathread]
Static void main (string [] ARGs)
{
ADOX. catalogclass cat = new ADOX. catalogclass ();
Cat. Create ("provider = Microsoft. Jet. oledb.4.0;" +
"Data Source = D :\\ accessdb \ newmdb. mdb;" +
"Jet oledb: Engine type = 5 ");
Console. writeline ("database created successfully ");
Cat = NULL;
}
}
}
// The Asp.net code is as follows:
Private void page_load (Object sender, system. eventargs E)
{
ADOX. catalogclass cat = new ADOX. catalogclass ();
Cat. Create ("provider = Microsoft. Jet. oledb.4.0;" +
"Data Source = C: // database // newmdb. mdb;" +
"Jet oledb: Engine type = 5 ");
Cat = NULL;
Response. Write ("OK ");
2. the SQL server code is as follows:
Sqlconnection conn = new sqlconnection ("Server = lemoncat007; uid = sa; Pwd = GTT ");
Conn. open ();
Sqlcommand cmd = new sqlcommand ("create database test", Conn );
Cmd. executenonquery ();
3. You can also create a procedure to write the statements for creating the database to it and then execute

Use C # To dynamically create an Access database

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= NewCatalog ();
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= NewCatalog ();
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 = New ADOX. Table ();
Table. Name = " Firsttable " ;

ADOX. 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.
key settings are as follows: Table. keys. append ("firsttableprimarykey", keytypeenum. as shown in adkeyprimary, column, null, null), if it is a foreign key, you must also set the associated table and associated fields, that is, the last two fields of the append method.
You can also create indexes and views by referring to the code above.

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.