在C# + Access + ADOX环境下创建数据库和数据表

首先在COM选项卡中选择添加:

Microsoft ActiveX Data Objects 2.8 Library

Microsoft ADO Ext. 2.8  for DDL  and Security

并在代码窗口中,添加

using ADOX;

 

 

创建数据库

            ADOX.CatalogClass catalog1 = new CatalogClass();
            catalog1.Create(@"provider = Microsoft.Jet.oledb.4.0; data source = D:/database/QSet.mdb;");
            ADODB.Connection conn = new ADODB.Connection();

                conn.Open(@"provider = Microsoft.Jet.oledb.4.0; data source = D:/database/QSet.mdb;",
                    null, null, -1);
                catalog1.ActiveConnection = conn;
创建数据表
                ADOX.TableClass table1 = new TableClass();
                //table1.ParentCatalog = catalog1;
                table1.Name = "QTable";
                //添加QNO字段
                ADOX.ColumnClass column1 = new ColumnClass();
                column1.ParentCatalog = catalog1;
                column1.Type = ADOX.DataTypeEnum.adVarWChar;
                column1.Name = "QNO";
                column1.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
                column1.DefinedSize = 50;
                table1.Columns.Append(column1, ADOX.DataTypeEnum.adVarWChar, 50);
                 table1.Keys.Append("FirstTablePrimaryKey",KeyTypeEnum.adKeyPrimary,column1,null,null);
                table1.Columns.Append("QCourse",ADOX.DataTypeEnum.adVarWChar,20);
                table1.Columns.Append("QType",ADOX.DataTypeEnum.adVarWChar,20);
                table1.Columns.Append("QKnowledge",ADOX.DataTypeEnum.adVarWChar,50);
                table1.Columns.Append("QDifficulty",ADOX.DataTypeEnum.adVarWChar,50);
                              table1.Columns.Append("QSummary",ADOX.DataTypeEnum.adLongVarWChar,255);
                table1.Columns.Append("QEditer",ADOX.DataTypeEnum.adVarWChar,20);
                table1.Columns.Append("QEditTime",ADOX.DataTypeEnum.adVarWChar,20);
                table1.Columns.Append("QUsedFrequency",ADOX.DataTypeEnum.adDouble,20);
                table1.Columns.Append("QLastTime",ADOX.DataTypeEnum.adDate,20);
                table1.Columns.Append("QPath",ADOX.DataTypeEnum.adVarWChar,255);
                table1.Columns.Append("QIncrease",ADOX.DataTypeEnum.adVarWChar,20);

将数据表添加到数据库中,只有添加这步才能在数据库中最终添加上数据表。
                catalog1.Tables.Append(table1);
                table1 = null;

你可能感兴趣的:(在C# + Access + ADOX环境下创建数据库和数据表)