Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/16 9:32:31
如何在.NET框架下创建Access数据库和表?_[Asp.Net教程]

如何在.NET框架下创建Access数据库和表?_[Asp.Net教程]

using System;
using ADOX;


namespace WebPortal
{
///


/// CreateAccessDB 的摘要说明。
/// 对于不同版本的ADO,需要添加不同的引用
/// 请添加引用Microsoft ADO Ext. 2.7 for DDL and Security
/// 请添加引用Microsoft ADO Ext. 2.8 for DDL and Security
///

public class CreateAccessDB : System.Web.UI.Page
{
private void Page_Load(object sender, System.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);


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


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


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}


///


/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}


来源:孟宪会之精彩世界







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759