C#操作Access数据库(创建&修改结构)

想要在程序中控制Access,不是数据,而是Access数据库的表结构啊,字段啊..就要用到ADOX


所以先要在解决方案中引用之,方法如下:
解决方案资源管理器 --> 引用 --> (右键)添加引用 --> COM --> Microsoft ADO Ext. 2.8 for DDL and Security  

注意:

当创建ACCESS数据库的时候,会自动创建一个连接,为了释放.ldb文件,必须关闭这一连接。而这个连接是ADODB类的,所以很多人一直都找不到释放连接的方法。 

方法如下: 引用 --> (右键)添加引用 --> COM --> Microsoft ActiveX Data Objects 2.8 Library

有了这个两个COM组件后,就可以开始一系列操作啦。。。


一.创建Access数据库 

string dbName = "E:\\Temp\\" + DateTime.Now.Millisecond.ToString() + ".mdb";//注意扩展名必须为mdb,否则不能插入表
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 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);

四.增加一个文本字段

ADOX.ColumnClass col2 = new ADOX.ColumnClass();
col2.ParentCatalog = cat;
col2.Name = "Description";
col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25);

下面的这句很重要哦:

cat.Tables.Append(tbl); //把表加入数据库(非常重要)

五.打开修改Access数据库

ADODB.Connection conn = new ADODB.Connection();//ADO连接对象
conn.Open("Provider=Microsoft.Jet.OleDb.4.0;Data Source=Interop.Portal.dll", "", "", 0);
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.ActiveConnection = conn;//设置活动的连接对象
下面的这句非常重要哦: (创建&修改都需要)
//转换为ADO连接,并关闭
(cat.ActiveConnection as ADODB.Connection).Close();
cat.ActiveConnection = null;
cat = null;

附件:代码

/// <summary> 
   /// CreateAccessDB 的摘要说明。 
   /// 对于不同版本的ADO,需要添加不同的引用 
   /// 请添加引用Microsoft ADO Ext. 2.7 for DDL and Security 
   /// 请添加引用Microsoft ADO Ext. 2.8 for DDL and Security 
   /// </summary> 
   
   protected void Page_Load(object sender, EventArgs e) 
   { 
   //为了方便测试,数据库名字采用比较随机的名字,以防止添加不成功时还需要重新启动IIS来删除数据库。 
   string dbName = "D:\\NewMDB" + DateTime.Now.Millisecond.ToString() + ".mdb"; 
   ADOX.CatalogClass cat = new ADOX.CatalogClass(); 
   cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";"); 
   Response.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); 
   
   //增加一个文本字段 
   ADOX.ColumnClass col2 = new ADOX.ColumnClass(); 
   col2.ParentCatalog = cat; 
   col2.Name = "Description"; 
   col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false; 
   tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25); 
   
   //增加数字字段 
   ADOX.ColumnClass col3 = new ADOX.ColumnClass(); 
   col3.ParentCatalog = cat; 
   col3.Name = "数字"; 
   col3.Type = DataTypeEnum.adDouble; 
   col3.Properties["Jet OLEDB:Allow Zero Length"].Value = false; 
   tbl.Columns.Append(col3, ADOX.DataTypeEnum.adDouble, 666); 
   
   //增加Ole字段 
   ADOX.ColumnClass col4 = new ADOX.ColumnClass(); 
   col4.ParentCatalog = cat; 
   col4.Name = "Ole类型"; 
   col4.Type = DataTypeEnum.adLongVarBinary; 
   col4.Properties["Jet OLEDB:Allow Zero Length"].Value = false; 
   tbl.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 0); 
   
   
   //设置主键 
   tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", ""); 
   cat.Tables.Append(tbl); 
   
   msg.Text = ("<br>数据库表:" + tbl.Name + "已经创建成功!"); 
   
   System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl); 
   System.Runtime.InteropServices.Marshal.ReleaseComObject(cat); 
   tbl = null; 
   cat = null; 
   GC.WaitForPendingFinalizers(); 
   GC.Collect(); 
   } 








猜你喜欢

转载自blog.csdn.net/xiatiancc/article/details/80405232