程序中为Access数据库创建表,其中一列为自动编号类型

using System;
using ADOX;

...
string dbName = "C:\\MyMDB.mdb";
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName +";");
Console.Write("数据库:" + dbName + "已经创建成功!");
ADOX.TableClass tbl = new ADOX.TableClass();
tbl.ParentCatalog = cat;
tbl.Name="MyTable";

//增加一个自动增长的字段
ADOX.ColumnClass col = new ADOX.ColumnClass();
col.ParentCatalog = cat;
col.Type=ADOX.DataTypeEnum.adInteger; // 必须先设置字段类型
col.Name = "id";
col.Properties["Jet OLEDB:Allow Zero Length"].Value= false;
col.Properties["AutoIncrement"].Value= true;
tbl.Columns.Append (col,ADOX.DataTypeEnum.adInteger,0);

//设置主键
tbl.Keys.Append("PrimaryKey",ADOX.KeyTypeEnum.adKeyPrimary,"id","","");
cat.Tables.Append (tbl);

Console.Write("
数据库表:" + tbl.Name + "已经创建成功!");
tbl=null;
cat = null;

你可能感兴趣的:(C++,c,Microsoft,C#,Access)