二.新建一个表
ADOX.TableClass tbl = new ADOX.TableClass();
tbl.ParentCatalog = cat;
tbl.Name = "MyTable";
三.增加一个自动增长的字段
tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0);
四.增加一个文本字段
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25);
下面的这句很重要哦:
cat.Tables.Append(tbl); //把表加入数据库(非常重要)
五.打开修改Access数据库
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.ActiveConnection = conn;//设置活动的连接对象
下面的这句非常重要哦: (创建&修改都需要)
//转换为ADO连接,并关闭
(cat.ActiveConnection as ADODB.Connection).Close();
cat.ActiveConnection = null;
cat = null;
//添加两个com组件引用
//Microsoft ADO Ext. 2.8 for DDL and Security
//Microsoft ActiveX Data Objects 2.8 Library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADOX;
using System.IO;
namespace WebRequestTest.Common
{
public static class AccessDbHelper
{
/// <summary>
/// 创建access数据库
/// </summary>
/// <param name="filePath">数据库文件的全路径,如 D:\\NewDb.mdb</param>
public static bool CreateAccessDb(string filePath)
{
ADOX.Catalog catalog = new Catalog();
if (!File.Exists(filePath))
{
try
{
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;DData Source=" + filePath + ";Jet OLEDB:Engine Type=5");
}
catch (System.Exception ex)
{
return false;
}
}
return true;
}
/// <summary>
/// 在access数据库中创建表
/// </summary>
/// <param name="filePath">数据库表文件全路径如D:\\NewDb.mdb 没有则创建 </param>
/// <param name="tableName">表名</param>
/// <param name="colums">ADOX.Column对象数组</param>
public static void CreateAccessTable(string filePath, string tableName, params ADOX.Column[] colums)
{
ADOX.Catalog catalog = new Catalog();
//数据库文件不存在则创建
if (!File.Exists(filePath))
{
try
{
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Jet OLEDB:Engine Type=5");
}
catch (System.Exception ex)
{
}
}
ADODB.Connection cn = new ADODB.Connection();
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath, null, null, -1);
catalog.ActiveConnection = cn;
ADOX.Table table = new ADOX.Table();
table.Name = tableName;
foreach (var column in colums)
{
table.Columns.Append(column);
}
// column.ParentCatalog = catalog;
//column.Properties["AutoIncrement"].Value = true; //设置自动增长
//table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null); //定义主键
catalog.Tables.Append(table);
cn.Close();
}
//========================================================================================调用
//ADOX.Column[] columns = {
// new ADOX.Column(){Name="id",Type=DataTypeEnum.adInteger,DefinedSize=9},
// new ADOX.Column(){Name="col1",Type=DataTypeEnum.adWChar,DefinedSize=50},
// new ADOX.Column(){Name="col2",Type=DataTypeEnum.adLongVarChar,DefinedSize=50}
// };
// AccessDbHelper.CreateAccessTable("d:\\111.mdb", "testTable", columns);
}
}
string dbName = "F:/FirstCatalog.mdb";
//string dbName = "F:\\FirstCatalog" + DateTime.Now.Millisecond.ToString() + ".mdb";
// 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");
//Response.Write("数据库:" + dbName + "已经创建成功!");
ADOX.Table table = new ADOX.Table();
table.ParentCatalog = catlog;
table.Name = "FirstTable";
//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 + "已经创建成功!");
System.Runtime.InteropServices.Marshal.ReleaseComObject(table);
System.Runtime.InteropServices.Marshal.ReleaseComObject(catlog);
table = null;
catlog = null;
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine("第一次创建");
Console.ReadKey();
}
else
{
Console.WriteLine("数据库已存在");
Console.ReadKey();
}