C # operate the ACCESS database (create and modify the structure)

Source: Internet
Author: User

ToProgramAccess control, not data, but the table structure of the Access database. The field is required.

Therefore, you must first reference it in the solution as follows:
Solution resource manager --> reference --> (right-click) Add reference --> com -->Microsoft ADO Ext. 2.8 for DDL and security 

 

Note:

When an Access database is created, a connection is automatically created. To release the. LDB file, the connection must be closed. This connection is from the ADODB class, so many people have never been able to find a way to release the connection.

The method is as follows: Reference --> (right-click) Add reference --> com -->Microsoft ActiveX Data Objects 2.8 Library

 

With these two COM components, you can start a series of operations...

 

1. Create an Access database

String Dbname =   " E: \ temp \\ "   + Datetime. Now. millisecond. tostring () +   " . MDB " ; // Note that the extension must be MDB; otherwise, the table cannot be inserted.
ADOX. catalogclass cat =   New ADOX. catalogclass ();
Cat. Create ( " Provider = Microsoft. Jet. oledb.4.0; Data Source = "   + Dbname +   " ; " );

II.Create a table

ADOX. tableclass TBL =   New ADOX. tableclass ();
TBL. parentcatalog = CAT;
TBL. Name =   " Mytable " ;

 

III.Add an Automatically increasing field

ADOX. columnclass col =   New ADOX. columnclass ();
Col. parentcatalog = CAT;
Col. Type = ADOX. datatypeenum. adinteger; // The field type must be set first.
Col. Name =   " ID " ;
Col. properties [ " Jet oledb: Allow Zero Length " ]. Value =   False ;
Col. properties [ " Autoincrement " ]. Value =   True ;
TBL. Columns. append (COL, ADOX. datatypeenum. adinteger, 0 );

 

4.Add a text field

ADOX. columnclass col2 =   New ADOX. columnclass ();
Col2.parentcatalog = CAT;
Col2.name =   " Description " ;
Col2.properties [ " Jet oledb: Allow Zero Length " ]. Value =   False ;
TBL. Columns. append (col2, ADOX. datatypeenum. advarchar, 25 );


The following sentence is very important:

Cat. Tables. append (TBL ); // Add tables to the database (very important)

5. Open and modify the ACCESS database

ADODB. Connection Conn =   New ADODB. Connection (); // ADO connection object
Conn. Open ( " Provider = Microsoft. Jet. oledb.4.0; Data Source = InterOP. Portal. dll " , "" , "" , 0 );
ADOX. catalogclass Cat =   New ADOX. catalogclass ();
Cat. activeconnection = Conn; // Set the active connection object

The following sentence is very important: (creation and modification are required)

// Convert to ADO connection and close
(Cat. activeconnection As ADODB. Connection). Close ();
Cat. activeconnection =   Null ;
Cat =   Null ;

 

 

 

 

 

 

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.