zoukankan      html  css  js  c++  java
  • 使用C#创建Access2010或Access2007 accdb 数据库文件

    C# 代码
    /*
      * 前提条件,你需要先安装Microsoft Access Database Engine 2010 Redistributable 下载地址:
      * http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=c06b8369-60dd-4b64-a44b-84b371ede16d&displaylang=zh-cn
      * 需要注意的是:下载的版本跟你的程序编译的.NET版本一致,而不是跟操作系统的版本一致。
      *
      * 需要添加引用 Microsoft ADO Ext. 2.8 for DDL and Security 文件位置:C:\Program Files (x86)\Common Files\System\ado\msadox28.tlb
      * 或者 Microsoft ADO Ext. 6.0 for DDL and Security 文件位置:C:\Program Files (x86)\Common Files\System\ado\msadox.dll
    */

    //数据库名称可路径
    String accdb = "K:\\xxx.accdb";
    if (System.IO.File.Exists(accdb)) System.IO.File.Delete(accdb);
    ADOX.Catalog cat = new ADOX.Catalog();
    cat.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + accdb);

    ADOX.Table tbl = new ADOX.Table();
    tbl.ParentCatalog = cat;
    tbl.Name = "Table1";

    //增加一个自动增长的字段
    ADOX.Column col = new ADOX.Column();
    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.Column col2 = new ADOX.Column();
    col2.ParentCatalog = cat;
    col2.Name = "Description";
    col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
    tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25);

    //增加数字字段
    ADOX.Column col3 = new ADOX.Column();
    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.Column col4 = new ADOX.Column();
    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);

    System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(cat);
    tbl = null;
    cat = null;
    GC.WaitForPendingFinalizers();
    GC.Collect();
    MessageBox.Show("创建完成!");

  • 相关阅读:
    1.ok6410移植bootloader,移植u-boot,学习u-boot命令
    ok6410按键中断编程,linux按键裸机
    四. jenkins部署springboot项目(1)--window环境
    一.jenkins安装(windows环境)
    oracle服务端导出/导入方式expdp/impdp
    linux 日志文件查看
    linux kafka进程挂了 自动重启
    kafka manager遇到的一些问题
    if条件语句
    shell脚本的条件测试与比较
  • 原文地址:https://www.cnblogs.com/zhihai/p/2349548.html
Copyright © 2011-2022 走看看