• 创建access数据库表demo的封装


    1.创建类

    public void CreateBiao(ADOX.Catalog catlog,ADOX.Table table)
    {

    //StuId Column(AutoIncrement ) //增加一个自动增长的字段
    ADOX.Column col1 = new ADOX.Column();
    col1.ParentCatalog = catlog;
    col1.Type = ADOX.DataTypeEnum.adInteger;
    col1.Name = "StuId";
    col1.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
    col1.Properties["AutoIncrement"].Value = true;
    table.Columns.Append(col1, ADOX.DataTypeEnum.adInteger, 0);

    ////备注:
    //ADOX.Column c = new ADOX.Column();
    //c.ParentCatalog = catlog;
    //c.Type = ADOX.DataTypeEnum.adLongVarWChar; //这句不能少,并且位置必须在其它属性前面,否则会报错。
    //c.Name = "list1";
    //c.Properties["Jet OLEDB:Allow Zero Length"].Value = true;
    //table.Columns.Append(c, ADOX.DataTypeEnum.adLongVarWChar, 16);

    //Name Column //增加一个文本字段
    ADOX.Column col2 = new ADOX.Column();
    col2.ParentCatalog = catlog;
    col2.Name = "StuName";
    col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
    table.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 25);

    //Age Column //增加数字字段
    ADOX.Column col3 = new ADOX.Column();
    col3.ParentCatalog = catlog;
    col3.Name = "Stuage";
    col3.Type = DataTypeEnum.adDouble;
    col3.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
    table.Columns.Append(col3, ADOX.DataTypeEnum.adDouble, 666);

    //增加Ole字段
    ADOX.Column col4 = new ADOX.Column();
    col4.ParentCatalog = catlog;
    col4.Name = "Ole类型";
    col4.Type = DataTypeEnum.adLongVarBinary;
    col4.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
    table.Columns.Append(col4, ADOX.DataTypeEnum.adLongVarBinary, 0);

    //Primary 设置主键
    table.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "StuId", "", "");
    catlog.Tables.Append(table);

    //msg.Text = ("<br>数据库表:" + tbl.Name + "已经创建成功!");



    }

    2.main函数

    string dbName = "F:/FirstCatalog.mdb";
    //string dbName = "F:\FirstCatalog" + DateTime.Now.Millisecond.ToString() + ".mdb";
    //string dbName = System.Windows.Forms.Application.StartupPath + "\FirstCatalog.mdb";

    ADOX.Catalog catlog = new ADOX.Catalog();
    if (!File.Exists(dbName))
    {
    catlog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName + ";" + "Jet OLEDB:Engine Type=5");

    Biao b =new Biao();

    ADOX.Table table = new ADOX.Table();
    table.ParentCatalog = catlog;
    table.Name = "FirstTable";
    b.CreateBiao(catlog,table);

    ADOX.Table table1 = new ADOX.Table();
    table1.ParentCatalog = catlog;
    table1.Name = "TwoTable";
    b.CreateBiao(catlog, table1);

    ADOX.Table table2 = new ADOX.Table();
    table2.ParentCatalog = catlog;
    table2.Name = "ThreeTable";
    b.CreateBiao(catlog, table2);

    ADOX.Table table3 = new ADOX.Table();
    table3.ParentCatalog = catlog;
    table3.Name = "FourTable";
    b.CreateBiao(catlog, table3);

    System.Runtime.InteropServices.Marshal.ReleaseComObject(table);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(table1);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(table2);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(table3);

    System.Runtime.InteropServices.Marshal.ReleaseComObject(catlog);

    table = null;
    table1 = null;
    table2 = null;
    table3 = null;
    catlog = null;
    GC.WaitForPendingFinalizers();
    GC.Collect();

    Console.WriteLine("第一次创建");
    Console.ReadKey();

    }
    else
    {
    Console.WriteLine("数据库已存在");
    Console.ReadKey();
    }

  • 相关阅读:
    SecureCRT 连接 虚拟机Linux 命令
    如何使用secureCRT连接vmware中的虚拟主机?
    SecureCRT8.1+SecureCRT_keygen完成注册
    常用python机器学习库总结
    Torch7在Ubuntu下的安装与配置
    朴素贝叶斯算法 & 应用实例
    编写MR代码中,JAVA注意事项
    march.
    Docker CentOS 7.2镜像systemd问题解决办法
    Docker 基础命令 简要入门
  • 原文地址:https://www.cnblogs.com/liuxingleiyu/p/6117445.html
Copyright © 2020-2023  润新知