Dynamically create an ACCESS database and its table structure

Source: Internet
Author: User
1. dynamically create a table structure, introduce two dynamic libraries in com, one is ADOX, the other is ADODB, and the other is ADOX. dll and Interop. ADODB. dll2. Add a button in wfm. The button event is as follows: m_path is the path of the current program running. You can write an absolute path, which is a relative path. Privatevoidbutton#click (objectsend

1. dynamically create a table structure, introduce two dynamic libraries in com, one is ADOX, the other is ADODB, and the other is ADOX. dll and Interop. ADODB. dll 2. add a button in wfm. The button event is as follows: m_path is the path of the current program running. You can write absolute paths, which are relative paths. Private void button#click (object send

1. dynamically create the table structure and introduce two dynamic libraries in com. One is ADOX and the other is ADODB. dll, ADOX. dll and Interop. ADODB. dll.

2. Add a button in wfm. The button event is as follows:

M_path is the path of the current program running, which can be an absolute path. It is a relative path.

Private void button#click (object sender, EventArgs e)
{
String strdb = m_path + "\ DomTest. mdb ";


If (File. Exists ("" + strdb + "") // first checks whether the database Exists. if not, create
{

MessageBox. Show ("the current database exists. Please delete it first", "message prompt ");
Return;
}
ADOX. Catalog catalog = new Catalog ();

Catalog. Create ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = '" + strdb + "'; Jet OLEDB: Engine Type = 5 ");

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

Cn. Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = '" + strdb + "'", null, null,-1 );
Catalog. ActiveConnection = cn;

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

ADOX. Column column = new ADOX. Column ();
Column. ParentCatalog = catalog;
Column. Name = "RecordId"; // column Name
Column. Type = DataTypeEnum. adInteger; // column data Type
Column. DefinedSize = 9; // size
Column. Properties ["AutoIncrement"]. Value = true; // whether the attribute is automatically added
Table. Columns. Append (column, DataTypeEnum. adInteger, 9); // Add the first column
Table. Keys. Append ("TablePrimaryKey", KeyTypeEnum. adKeyPrimary, column, null, null );
Table. Columns. Append ("Name", DataTypeEnum. adVarWChar, 50); // Add the second column
Table. Columns. Append ("Age", DataTypeEnum. adInteger, 9); // Add the third column
Table. Columns. Append ("Birthday", DataTypeEnum. adDate, 0); // Add the fourth column
Catalog. Tables. Append (table); // Add the current table

Cn. Close ();
MessageBox. Show ("the database is created successfully and its location is stored in" + m_path, "");
}

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.