使用ADOX創(chuàng)建Access數(shù)據(jù)庫和表
2024-09-07 19:04:57
供稿:網(wǎng)友
using system;
using adox;
namespace webportal
{
/// <summary>
/// createaccessdb 的摘要說明。
/// 對于不同版本的ado,需要添加不同的引用
/// 請?zhí)砑右胢icrosoft ado ext. 2.7 for ddl and security
/// 請?zhí)砑右胢icrosoft ado ext. 2.8 for ddl and security
/// </summary>
public class createaccessdb : system.web.ui.page
{
private void page_load(object sender, system.eventargs e)
{
//為了方便測試,數(shù)據(jù)庫名字采用比較隨機(jī)的名字,以防止添加不成功時還需要重新啟動iis來刪除數(shù)據(jù)庫。
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("數(shù)據(jù)庫:" + dbname + "已經(jīng)創(chuàng)建成功!");
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; // 必須先設(shè)置字段類型
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);
//設(shè)置主鍵
tbl.keys.append("primarykey",adox.keytypeenum.adkeyprimary,"id","","");
cat.tables.append (tbl);
response.write("<br>數(shù)據(jù)庫表:" + tbl.name + "已經(jīng)創(chuàng)建成功!");
tbl=null;
cat = null;
}
#region web 窗體設(shè)計器生成的代碼
override protected void oninit(eventargs e)
{
//
// codegen: 該調(diào)用是 asp.net web 窗體設(shè)計器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。