添加引用“Microsoft ADO Ext. 2.8 for DDL and Security”、“Microsoft ActiveX Data Objects 2.8 Library”
static void Main(string[] args)
{
ADOX.Catalog cat = new ADOX.Catalog();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:\\NewMDB.mdb;" + "Jet OLEDB:Engine Type=5");
ADODB.Connection conn = new ADODB.Connection();
conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\NewMDB.mdb;", null, null, -1);
cat.ActiveConnection = conn;
ADOX.Table table = new ADOX.Table(); //创建一个表
table.Name = "FirstTable";
ADOX.Column col = new ADOX.Column(); //创建一个自动增长的列
col.ParentCatalog = cat;
col.Name = "ID";
col.Type = ADOX.DataTypeEnum.adInteger;
col.DefinedSize = 9;
col.Properties["AutoIncrement"].Value = true; //设置自增长
table.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 9);
table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, col, null, null); //设置列为主键
table.Columns.Append("CustomerName", ADOX.DataTypeEnum.adVarWChar, 50); //追加一个Text类型的列
table.Columns.Append("Age", ADOX.DataTypeEnum.adInteger, 9); //追加一个Int类型的列
table.Columns.Append("Birthday", ADOX.DataTypeEnum.adDate, 0); //追加一个Date类型的列
cat.Tables.Append(table); //将表追加到数据库中
conn.Close();
Console.WriteLine("Database created successfully.");
}