{
//为了方便测试,数据库名字采用比较随机的名字,以防止添加不成功时还需要重新启动IIS来删除数据库。
string time = DateTime.Now.Year.ToString()
if (DateTime.Now.Month <10)//判断当前月是否小于10
{
time += "0" + DateTime.Now.Month
}
else
{
time += DateTime.Now.Month
}
time += DateTime.Now.Day
string apppath = System.Web.HttpContext.Current.Request.ApplicationPath
string dbName = System.Web.HttpContext.Current.Server.MapPath("\\" + apppath + "\\NewMDB\\CGMDB" + time + ".mdb")
ADOX.CatalogClass cat = new ADOX.CatalogClass()
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0Data Source=" + dbName + "")
//ADODB.Connection cn = new ADODB.Connection()
//cn.Open("Provider=Microsoft.Jet.OLEDB.4.0Data Source=" + dbName + "", null, null, -1)
// cat.ActiveConnection = cn
ADOX.TableClass tbl = new ADOX.TableClass()
tbl.ParentCatalog = cat
tbl.Name = "WTMDBTable"
ADOX.ColumnClass col2 = new ADOX.ColumnClass()
col2.ParentCatalog = cat
col2.Name = "CheckSignNo"
col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 50)
//增加一个文本字段
ADOX.ColumnClass col3 = new ADOX.ColumnClass()
col3.ParentCatalog = cat
col3.Name = "CarNo"
col3.Properties["Jet OLEDB:Allow Zero Length"].Value = false
tbl.Columns.Append(col3, ADOX.DataTypeEnum.adVarChar, 50)
//增加一个文本字段
ADOX.ColumnClass col4 = new ADOX.ColumnClass()
col4.ParentCatalog = cat
col4.Name = "CheckDate"
col4.Properties["Jet OLEDB:Allow Zero Length"].Value = false
tbl.Columns.Append(col4, ADOX.DataTypeEnum.adVarChar, 50)
//增加一个文本字段
ADOX.ColumnClass col5 = new ADOX.ColumnClass()
col5.ParentCatalog = cat
col5.Name = "TownClass"
col5.Properties["Jet OLEDB:Allow Zero Length"].Value = false
tbl.Columns.Append(col5, ADOX.DataTypeEnum.adVarChar, 50)
//增加一个文本字段
ADOX.ColumnClass col6 = new ADOX.ColumnClass()
col6.ParentCatalog = cat
col6.Name = "Address"
col6.Properties["Jet OLEDB:Allow Zero Length"].Value = false
tbl.Columns.Append(col6, ADOX.DataTypeEnum.adVarChar, 200)
//增加一个文本字段
ADOX.ColumnClass col7 = new ADOX.ColumnClass()
col7.ParentCatalog = cat
col7.Name = "Action"
col7.Properties["Jet OLEDB:Allow Zero Length"].Value = false
tbl.Columns.Append(col7, ADOX.DataTypeEnum.adVarChar, 50)
//增加一个文本字段
ADOX.ColumnClass col8 = new ADOX.ColumnClass()
col8.ParentCatalog = cat
col8.Name = "CarType"
col8.Properties["Jet OLEDB:Allow Zero Length"].Value = false
tbl.Columns.Append(col8, ADOX.DataTypeEnum.adVarChar, 50)
//增加一个文本字段
ADOX.ColumnClass col9 = new ADOX.ColumnClass()
col9.ParentCatalog = cat
col9.Name = "Mark"
col9.Properties["Jet OLEDB:Allow Zero Length"].Value = false
tbl.Columns.Append(col9, ADOX.DataTypeEnum.adVarChar, 50)
//设置主键
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "CheckSignNo", "", "")
cat.Tables.Append(tbl)
System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl)
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat)
tbl = null
cat = null
GC.WaitForPendingFinalizers()
GC.Collect()
}
以上代码可以完成创建一个MDB数据库了
Set Catalog_object= Server.CreateObject("ADOX.Catalog")Catalog_object.Create("Provider=Microsoft.Jet.OLEDB.4.0Data Source=" & Server.MapPath("/jb51.net.mdb"))
可以创建出access数据库,在create函数中只需要提供“数据库文件的路径”。
刚创建的access数据库是个“空库”,其中没有任何表
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)