C # create an Access database and a data table

Source: Internet
Author: User

Because ADOX is used in the program, you must first reference it in the solution as follows:
Solution Explorer --> reference --> (right-click) Add reference --> com --> Microsoft ADO Ext. 2.8 for DDL and security

Private void btncreate_click (Object sender, eventargs E)
{
String dbname = "E: \ temp \" + datetime. Now. millisecond. tostring () + ". mdb ";
ADOX. catalogclass cat = new ADOX. catalogclass ();
Cat. Create ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + dbname + ";");
MessageBox. Show ("Database:" + dbname + "already created! ");

// Create a table
ADOX. tableclass TBL = new ADOX. tableclass ();
TBL. parentcatalog = cat;
TBL. Name = "mytable ";

// Add an Automatically increasing field
ADOX. columnclass Col = new ADOX. columnclass ();
Col. parentcatalog = cat;
Col. type = ADOX. datatypeenum. adinteger; // you must set the field type 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 );

// 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 );
Cat. Tables. append (TBL); // Add the table to the database (very important)
MessageBox. Show ("database table:" + TBL. Name + "already created! ");
TBL = 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.