网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 使用C#动态创建Access数据库及密码
内容
    以前工作中需要全新的Access数据库,可以复制数据库,也可以把新的数据库放到资源里面,用新数据库的时候释放出来,都感觉不爽,还是动态生成心理舒服。生成数据库要使用ADO,首先添加引用。
    using System.IO;
    using System.Data.OleDb; //连接Access数据库
    using ADOX;
    //引用COM:Microsoft ADO Ext. 2.8 for DDL and Security
    //添加引用:Microsoft ActioveX Data Objects 2.8 Library
    创建数据库:然后使用ADODB创建数据库,直接看代码:
    string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName;
    //创建数据库
    ADOX.Catalog catalog = new Catalog();
    try
    {
    catalog.Create(conn);
    }
    catch
    {}
    //连接数据库
    ADODB.Connection cn = new ADODB.Connection();
    cn.Open(conn, null, null, -1);
    catalog.ActiveConnection = cn;
    //新建表
    ADOX.Table table = new ADOX.Table();
    table.Name = "AdPlayList";
    ADOX.Column column = new ADOX.Column();
    column.ParentCatalog = catalog;
    column.Type = ADOX.DataTypeEnum.adInteger; // 必须先设置字段类型
    column.Name = "ID";
    column.DefinedSize = 9;
    column.Properties["AutoIncrement"].Value = true;
    table.Columns.Append(column, DataTypeEnum.adInteger, 0);
    //设置主键
    table.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "ID", "", "");
    table.Columns.Append("FileName", DataTypeEnum.adVarWChar, 50);
    table.Columns.Append("FileDate", DataTypeEnum.adDate, 0);
    table.Columns.Append("FileSize", DataTypeEnum.adInteger, 9);
    table.Columns.Append("OrderID", DataTypeEnum.adInteger, 9);
    table.Columns.Append("Sha1", DataTypeEnum.adVarWChar, 50);
    try
    {
    catalog.Tables.Append(table);
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    //此处一定要关闭连接,否则添加数据时候会出错
    table = null;
    catalog = null;
    Application.DoEvents();
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/18 9:06:53