C # determine access to build a library, build a table, file existence, etc.

Source: Internet
Author: User

1. Create a database

2. Determine if a table exists

3. Create a table

1.    CREATE DATABASE #region Access database operations
        private void Creatmdb (String dbName)
        {
             ADOX. Catalogclass cat = new ADOX. Catalogclass ();
            Cat. Create ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + DbName + ";");
           //adox. Catalog cat = new Catalog ();
           //cat. Create ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + DbName + "; Jet oledb:engine type=5 "); s
       }
         #endregion

   2.     #region Access database operations determine if a table exists in the database
         public bool Gettables (OleDbConnection conn)
        {
& nbsp;           int result = 0;
            DataTable schematable = conn. GetOleDbSchemaTable (OleDbSchemaGuid.Tables,
                                                                new object[] {null, NULL, NULL , "TABLE"});


if (schematable! = null)
{

for (Int32 row = 0; row < SchemaTable.Rows.Count; row++)
{
String col_name = schematable.rows[row]["table_name"]. ToString ();
if (col_name = = "Mychoosestock")
{
result++;
}
}
}
if (result = = 0)
return false;
return true;
}
#endregion

3. CREATE table #region Access database operations
Create a table for a new MDB, C # Operations Access creation table
Mdbhead is a ArrayList that stores the specific column names in table tables.
private void Createmdbtable (String mdbpath, String tableName, ArrayList mdbhead)
{
ADOX. Catalogclass cat = new ADOX. Catalogclass ();
String saccessconnection = @ "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "+ mdbpath;
Connection = new OleDbConnection (saccessconnection);
Connection. Open ();
ADODB. Connection cn = New ADODB. Connection ();
cn. Open (saccessconnection, NULL, NULL,-1);

            Cat. ActiveConnection = cn;
           //Create a new table, C # manipulate Access's creation table
             ADOX. Tableclass tbl = new ADOX. Tableclass ();
            tbl. ParentCatalog = cat;//directory
            tbl. Name = TableName;

Add an auto-growing field
ADOX. Columnclass col = new ADOX. Columnclass ();
Col. ParentCatalog = cat;
Col. Type = ADOX. Datatypeenum.adinteger; 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);

Add a text field
int size = Mdbhead.count;
for (int i = 0; i < size; i++)
{
Add a text field
ADOX. Columnclass col2 = new ADOX. Columnclass ();
Col2. ParentCatalog = cat;
Col2. Name = Mdbhead[i]. ToString (); The name of the column
Col2. properties["Jet oledb:allow Zero Length"]. Value = false;
Tbl. Columns.Append (col2, ADOX. Datatypeenum.advarwchar, 500);
}
Set Primary key
Tbl. Keys.append ("PrimaryKey", ADOX. Keytypeenum.adkeyprimary, "id", "", "" ");

Cat. Tables.append (TBL); This sentence joins the table to the database (very important), C # Operations The CREATE table for access
TBL = null;
cat = null;
Connection. Close ();
}
#endregion

4.

Using System.Data;
Using System.Data.OleDb;

Using ADOX; You need to add Microsoft ADO Ext. 6.0 ***security

protected override void Buttonok_click (object sender, EventArgs e)
{

private OleDbConnection connection;

Private OleDbDataAdapter dataAdapter;

String systempath = Environment.getfolderpath (Environment.SpecialFolder.System);
if (directory.exists (Systempath + "\\data") = = false)//create the file folder if it does not exist
{
Directory.CreateDirectory (Systempath + "\\data");
}
String Favoritestockmdb = Systempath + "\\data\\Data.mdb";
if (! File.exists (Favoritestockmdb)) Creatmdb (Favoritestockmdb);//Determine if there is a database, does not exist, create
Connection =
New OleDbConnection (@ "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + favoritestockmdb);
Connection. Open ();

if (! Gettables (Connection))
{
String table_name = "Mychoosestock";
ArrayList col_names = new ArrayList ();
Col_names. ADD ("Stockcode");
Createmdbtable (FAVORITESTOCKMDB, TABLE_NAME, col_names);
Connection. Open ();
}
DataAdapter = new OleDbDataAdapter ("SELECT * from Mychoosestock", connection);
DataSet Datasetselect = new DataSet ();
DataAdapter.Fill (Datasetselect, "sel");
DataTable datatable = datasetselect.tables["sel"];
for (int i = 0; i < DataTable.Rows.Count; i++)
{
Listselect.add (Datatable.rows[i][1]. ToString ());
}
OleDbCommand InsertCommand = connection. CreateCommand ();
InsertCommand.CommandText = "INSERT into Mychoosestock (Stockcode) VALUES (?)";
INSERTCOMMAND.PARAMETERS.ADD ("Stockcode", OleDbType.VarChar, 1024);
Insertcommand.prepare ();
Insertcommand.parameters[0]. Value = list[0];
Int32 row = Insertcommand.executenonquery ();
if (row! = 0)
{
Messageboxhelper.showinfo ("The collection is successful!" ");
}
Else
{
Messageboxhelper.showinfo ("Collection failed! ");
}
Connection. Close ();

}

C # determine access to build a library, build a table, file existence, etc.

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.