在.NET 2.0框架下动态创建Access数据库和表时的注意事项

来源:互联网 发布:淘宝主图尺寸 编辑:程序博客网 时间:2024/05/22 04:46

在以前的文章《如何在.NET框架下创建Access数据库和表?》中提供的方法,在.NET 2.0下无效,所有的字段类型都变成了文本类型,不知道微软改变了什么东西。提醒大家注意,需要加ADOX.ColumnClass.Type = DataTypeEnum.adLongVarBinary属性。下面将修正后的代码发布如下。

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">
  
//
  /// CreateAccessDB 的摘要说明。
  /// 对于不同版本的ADO,需要添加不同的引用
  /// 请添加引用Microsoft ADO Ext. 2.7 for DDL and Security
  /// 请添加引用Microsoft ADO Ext. 2.8 for DDL and Security
  //


  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 
= ("
数据库表:
" + 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>
VB.NET:
<%@ Page Language="VB" %>

<%@ 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">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim dbName As String 
= "D:NewMDB" + DateTime.Now.Millisecond.ToString + ".mdb"
    Dim cat As ADOX.CatalogClass 
= New ADOX.CatalogClass
    cat.Create(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";")
    Response.Write(
"数据库:" + dbName + "已经创建成功!")
    Dim tbl As ADOX.TableClass 
= New ADOX.TableClass
    tbl.ParentCatalog 
= cat
    tbl.Name 
= "MyTable"
    Dim col As ADOX.ColumnClass 
= 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)
    Dim col2 As ADOX.ColumnClass 
= 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)
    Dim col3 As ADOX.ColumnClass 
= 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)
    Dim col4 As ADOX.ColumnClass 
= New ADOX.ColumnClass
    col4.ParentCatalog 
= cat
    col4.Name 
= "Ole类型"
    col4.Type 
= DataTypeEnum.adLongVarBinary
    tbl.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 
0)
    tbl.Keys.Append(
"PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id""""")
    cat.Tables.Append(tbl)
    msg.Text 
= ("
数据库表:
" + tbl.Name + "已经创建成功!")
    System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(cat)
    tbl 
= Nothing
    cat 
= Nothing
    GC.WaitForPendingFinalizers()
    GC.Collect()
  End Sub
script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  
<title>在.NET框架下动态创建Access数据库和表title>
head>
<body>
  
<form id="form1" runat="server">
    
<asp:Label ID="msg" runat="server" />
  
form>
body>
html>

【Update】下面的代码添加对现有数据库的字段添加:

<%@ Page Language="C#" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  
/// 
  
/// CreateAccessDB 的摘要说明。
  
/// 对于不同版本的ADO,需要添加不同的引用
  
/// 请添加引用: Microsoft ADO Ext. 2.7 for DDL and Security
  
/// 请添加引用: Microsoft ADO Ext. 2.8 for DDL and Security
  
/// 另外,如果修改数据库,需要添加引用: Microsoft ActiveX Data Objects 2.8 Library
  
/// 


  
protected void Page_Load( object sender, EventArgs e )
  {
    
//为了方便测试,数据库名字采用比较随机的名字,以防止添加不成功时还需要重新启动IIS来删除数据库。
    string dbName = Server.MapPath("~"+ "/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 
= ADOX.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 
= ADOX.DataTypeEnum.adLongVarBinary;
    col4.Properties[
"Jet OLEDB:Allow Zero Length"].Value = false;
    tbl.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 
0);

    Response.Write(
"

得到各个列的名字:

");
    
for (int i = 0 ; i < tbl.Columns.Count ; i++)
    {
      Response.Write(
"
  • " + i.ToString() + " = " + tbl.Columns[i].Name + " 类型:" + tbl.Columns[i].Type.ToString());
        }

        Response.Write(
    "

    删除 Description 列:

    ");
        tbl.Columns.Delete(
    "Description");
        tbl.Columns.Refresh();

        Response.Write(
    "

    修改 “数字类型字段” 列名字:

    ");
        ADOX.ColumnClass col5 
    = (ADOX.ColumnClass)tbl.Columns["数字类型字段"];
        col5.Name 
    = "新名字类型字段";
        tbl.Columns.Refresh();

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

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

        Response.Write(
    "

    修改现有数据库:

    ");
        
    //打开已经存在的数据库,添加字段
        string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName;
        ADODB.Connection cn 
    = new ADODB.Connection();
        cn.Open(ConnectionString, 
    nullnull0);
        cat 
    = new ADOX.CatalogClass();
        cat.ActiveConnection 
    = cn;
        Response.Write(
    "数据库:" + dbName + "已经打开成功!");

        ADOX.Table dbTable 
    = cat.Tables["MyTable"];

        
    //增加一个文本字段
        ADOX.Column newColumn = new ADOX.Column();
        newColumn.ParentCatalog 
    = cat;
        newColumn.Name 
    = "Address";
        newColumn.Properties[
    "Jet OLEDB:Allow Zero Length"].Value = false;    
        dbTable.Columns.Append(newColumn, ADOX.DataTypeEnum.adVarChar, 
    255);
        Response.Write(
    "
    数据库表:
    " + dbTable.Name + "修改成功!");
        cn.Close();

        System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(dbTable);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(cat);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(cn);

        col 
    = null;
        col2 
    = null;
        col3 
    = null;
        col4 
    = null;
        col5 
    = null;
        newColumn 
    = null;
            
        tbl 
    = null;      
        dbTable 
    = null;
        cat 
    = null;   
        
        GC.WaitForPendingFinalizers();
        GC.Collect();
      }
    script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      
    <title>创建、修改 Access 数据库title>
    head>
    <body>
      
    <form id="Mengxianhui" runat="server">form>
    body>
    html>



  • 原创粉丝点击