1.创建类
public void CreateBiao(ADOX.Catalog catlog,ADOX.Table table)
{
//StuId Column(AutoIncrement ) //增加一个自动增长的字段
ADOX.Column col1 = new ADOX.Column();
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;
table.Columns.Append(col1, ADOX.DataTypeEnum.adInteger, 0);
////备注:
//ADOX.Column c = new ADOX.Column();
//c.ParentCatalog = catlog;
//c.Type = ADOX.DataTypeEnum.adLongVarWChar; //这句不能少,并且位置必须在其它属性前面,否则会报错。
//c.Name = "list1";
//c.Properties["Jet OLEDB:Allow Zero Length"].Value = true;
//table.Columns.Append(c, ADOX.DataTypeEnum.adLongVarWChar, 16);
//Name Column //增加一个文本字段
ADOX.Column col2 = new ADOX.Column();
col2.ParentCatalog = catlog;
col2.Name = "StuName";
col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
table.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25);
//Age Column //增加数字字段
ADOX.Column col3 = new ADOX.Column();
col3.ParentCatalog = catlog;
col3.Name = "Stuage";
col3.Type = DataTypeEnum.adDouble;
col3.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
table.Columns.Append(col3, ADOX.DataTypeEnum.adDouble, 666);
//增加Ole字段
ADOX.Column col4 = new ADOX.Column();
col4.ParentCatalog = catlog;
col4.Name = "Ole类型";
col4.Type = DataTypeEnum.adLongVarBinary;
col4.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
table.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 0);
//Primary 设置主键
table.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "StuId", "", "");
catlog.Tables.Append(table);
//msg.Text = ("<br>数据库表:" + tbl.Name + "已经创建成功!");
}
2.main函数
string dbName = "F:/FirstCatalog.mdb";
//string dbName = "F:\FirstCatalog" + DateTime.Now.Millisecond.ToString() + ".mdb";
//string dbName = System.Windows.Forms.Application.StartupPath + "\FirstCatalog.mdb";
ADOX.Catalog catlog = new ADOX.Catalog();
if (!File.Exists(dbName))
{
catlog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";" + "Jet OLEDB:Engine Type=5");
Biao b =new Biao();
ADOX.Table table = new ADOX.Table();
table.ParentCatalog = catlog;
table.Name = "FirstTable";
b.CreateBiao(catlog,table);
ADOX.Table table1 = new ADOX.Table();
table1.ParentCatalog = catlog;
table1.Name = "TwoTable";
b.CreateBiao(catlog, table1);
ADOX.Table table2 = new ADOX.Table();
table2.ParentCatalog = catlog;
table2.Name = "ThreeTable";
b.CreateBiao(catlog, table2);
ADOX.Table table3 = new ADOX.Table();
table3.ParentCatalog = catlog;
table3.Name = "FourTable";
b.CreateBiao(catlog, table3);
System.Runtime.InteropServices.Marshal.ReleaseComObject(table);
System.Runtime.InteropServices.Marshal.ReleaseComObject(table1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(table2);
System.Runtime.InteropServices.Marshal.ReleaseComObject(table3);
System.Runtime.InteropServices.Marshal.ReleaseComObject(catlog);
table = null;
table1 = null;
table2 = null;
table3 = null;
catlog = null;
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine("第一次创建");
Console.ReadKey();
}
else
{
Console.WriteLine("数据库已存在");
Console.ReadKey();
}