C # create an Access database and a data table

Source: Internet
Author: User
Because Program ADOX is used, so you must first reference it in the solution. The method is as follows:
Solution Explorer --> reference --> (right-click) Add reference --> com --> Microsoft ADO Ext. 2.8 for DDL and security

1. ADOX Overview:
Microsoft & reg; ActiveX & reg; Data Objects extensions for Data Definition Language and Security (ADOX) is an extension of ADO objects and programming models. ADOX includes the objects used for mode creation and modification, as well as security. Because it is based on the object implementation mode operation, you can write it to be effective for various data sources.CodeIs independent of the differences in their original syntax.
ADOX is the extension library of core ADO objects. Other Objects exposed by it can be used to create, modify, and delete schema objects, such as tables and processes. It also includes security objects, which can be used to maintain users and groups and grant and revoke permissions on objects.
To use ADOX through vs, you need to create a reference to the ADOX Type Library. 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.

2. ADOX object model:
catalog:
you can use the following statement to create a database:
// create a database string
string dbname = "D: \ database \ firsttable. mdb ";
ADOX. catalogclass catlog = new ADOX. catalogclass ();
// or ADOX. catalog catlog = new ADOX. catalo(); catalogclass is a class, catalog is an interface.
catlog. create ("provider = Microsoft. jet. oledb.4.0; Data Source = "+ dbname +"; "+" jet oledb: Engine type = 5 ");
table objects include database tables with columns, indexes, and keywords:

We can create a table as follows:
ADOX. tableclass TBL = new ADOX. tableclass (); // or ADOX. Table TBL = new ADOX. Table ();
TBL. parentcatalog = catlog; // Database Name
TBL. Name = "mytable ";
Table attributes:

  • Use nameAttribute identification table.
  • Use TypeAttribute determines the table type.
  • Use columnsDatabase columns of the Set access table.
  • Use IndexesIndex of the Access Table of the set.
  • Use keysThe keyword used to access the table in the set.
  • Use parentcatalogAttribute specifies the catalog that owns the table.
    Columns object:
    You can use the following statement to create a column:
  • ADOX. columnclass firstcol = new ADOX. columnclass ();
    Firstcol. parentcatalog = catlog;
    Firstcol. type = ADOX. datatypeenum. adinteger;
    Firstcol. Name = "stuid ";
    Firstcol. properties ["jet oledb: Allow zero length"]. value = false;
    Firstcol. properties ["autoincrement"]. value = true;
    TBL. Columns. append (firstcol, ADOX. datatypeenum. adinteger, 0 );
    Columns attributes:
  • Use nameAttribute identifier column.
  • Use TypeAttribute specifies the Data Type of the column.
  • Use attributesDetermines whether the column is of a fixed length or contains a null value.
  • Use definedsizeAttribute specifies the maximum size of a column.
  • For numeric data values, use numericscaleMethod.
  • For numeric data values, use precisionAttribute specifies the maximum precision.
  • Use parentcatalog Attribute specifies the catalog that owns the column .
    Firstcol. properties ["jet oledb: Allow zero length"]. value = true; this sentence sets the attribute of this field. The allowed length can be 0, that is, the field in the database can be empty.
    Autoincrement literally means auto-increment. You can select Auto-increment fields in access, that is, auto-increment fields 1, 2, 3, and 4. You do not need to assign values.
    3. Complete example:
    Using system;
    Using system. Collections. Generic;
    Using system. text;
    Using ADOX;
    Namespace adoxcreatetable
    ...{
    Class Program
    ...{
    Static void main (string [] ARGs)
    ...{

    String dbname = "D: \ database \ firstcatalog. mdb ";
    ADOX. catalogclass catlog = new ADOX. catalogclass ();
    Catlog. Create ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + dbname + ";" + "jet oledb: Engine type = 5 ");

    ADOX. tableclass table = new ADOX. tableclass ();
    Table. parentcatalog = catlog;
    Table. Name = "firsttable ";

    // Stuid column (autoincrement)
    ADOX. columnclass col1 = new ADOX. columnclass ();
    Col1.parentcatalog = catlog;
    Col1.type = ADOX. datatypeenum. adinteger;
    Col1.name = "stuid ";
    Col1.properties ["jet oledb: Allow zero length"]. value = false;
    Col1.properties ["autoincrement"]. value = true;
    Note:
    ADOX. columnclass c = new ADOX. columnclass ();
    C. parentcatalog = catlog;
    C. Type = ADOX. datatypeenum. adlongvarwchar; // This statement must be followed by other attributes. Otherwise, an error is returned.
    C. Name = list1 ;
    C. properties ["jet oledb: Allow zero length"]. value = true;
    TBL. Columns. append (C, ADOX. datatypeenum. adlongvarwchar, 16 );

    // Name column
    ADOX. columnclass col2 = new ADOX. columnclass ();
    Col2.parentcatalog = catlog;
    Col2.name = "stuname ";
    Col2.properties ["jet oledb: Allow zero length"]. value = false;

    // Age Column
    ADOX. columnclass col3 = new ADOX. columnclass ();
    Col3.parentcatalog = catlog;
    Col3.name = "stuage ";
    Col3.type = datatypeenum. addouble;
    Col3.properties ["jet oledb: Allow zero length"]. value = false;

    // Primary
    Table. Keys. append ("primarykey", ADOX. keytypeenum. adkeyprimary, "stuid ","","");
    Table. Columns. append (col1, ADOX. datatypeenum. adinteger, 0 );
    Table. Columns. append (col3, ADOX. datatypeenum. addouble, 666 );
    Table. Columns. append (col2, ADOX. datatypeenum. advarchar, 25 );
    Catlog. Tables. append (table );

    System. runtime. interopservices. Marshal. releasecomobject (table );
    System. runtime. interopservices. Marshal. releasecomobject (catlog );
    Table = NULL;
    Catlog = NULL;
    GC. waitforpendingfinalizers ();
    GC. Collect ();
    }
    }
    }
  • 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.