想要在程序中控制Access,不是數據,而是Access數據庫的表結構啊,字段啊..就要用到ADOX
所以先要在解決方案中引用之,方法如下:
解決方案資源管理器 --> 引用 --> (右鍵)添加引用 --> COM --> Microsoft ADO Ext. 2.8 for DDL and Security
注意: <!--[endif]-->
當創建ACCESS數據庫的時候,會自動創建一個連接,為了釋放.ldb文件,必須關閉這一連接。而這個連接是ADODB類的,所以很多人一直都找不到釋放連接的方法。
方法如下: 引用 --> (右鍵)添加引用 --> COM --> Microsoft ActiveX Data Objects 2.8 Library
有了這個兩個COM組件后,就可以開始一系列操作啦。。。
一.創建Access數據庫
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create( " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + dbName + " ; " );
二.新建一個表
tbl.ParentCatalog = cat;
tbl.Name = " MyTable " ;
三.增加一個自動增長的字段
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 );
四.增加一個文本字段
col2.ParentCatalog = cat;
col2.Name = " Description " ;
col2.Properties[ " Jet OLEDB:Allow Zero Length " ].Value = false ;
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25 );
下面的這句很重要哦:
五.打開修改Access數據庫
conn.Open( " Provider=Microsoft.Jet.OleDb.4.0;Data Source=Interop.Portal.dll " , "" , "" , 0 );
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.ActiveConnection = conn; // 設置活動的連接對象
下面的這句非常重要哦: (創建&修改都需要)
(cat.ActiveConnection as ADODB.Connection).Close();
cat.ActiveConnection = null ;
cat = null ;
摘自昵稱:吳慶陽
C#:
<%@ Page Language="C#" %>
<%@ Import Namespace="ADOX" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
/// <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();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>在.NET框架下動態創建Access數據庫和表</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="msg" runat="server" />
</form>
</body>
</html>
本篇文章來源於 站長資訊網 原文鏈接:http://www.chinahtml.com/0612/aspnet-11664211899645.html