码迷,mamicode.com
首页 > 数据库 > 详细

C# 动态创建Access数据库及表

时间:2014-12-25 17:45:38      阅读:507      评论:0      收藏:0      [点我收藏+]

标签:

//添加两个com组件引用
//Microsoft ADO Ext. 2.8 for DDL and Security
//Microsoft ActiveX Data Objects 2.8 Library

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADOX;
using System.IO;

namespace WebRequestTest.Common
{
    public static class AccessDbHelper
    {
        /// <summary>
        /// 创建access数据库
        /// </summary>
        /// <param name="filePath">数据库文件的全路径,如 D:\\NewDb.mdb</param>
        public static bool CreateAccessDb(string filePath)
        {
            ADOX.Catalog catalog = new Catalog();
            if (!File.Exists(filePath))
            {
                try
                {
                    catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;DData Source=" + filePath + ";Jet OLEDB:Engine Type=5");
                }
                catch (System.Exception ex)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 在access数据库中创建表
        /// </summary>
        /// <param name="filePath">数据库表文件全路径如D:\\NewDb.mdb 没有则创建 </param>
        /// <param name="tableName">表名</param>
        /// <param name="colums">ADOX.Column对象数组</param>
        public static void CreateAccessTable(string filePath, string tableName, params ADOX.Column[] colums)
        {
            ADOX.Catalog catalog = new Catalog();
            //数据库文件不存在则创建
            if (!File.Exists(filePath))
            {
                try
                {
                    catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Jet OLEDB:Engine Type=5");
                }
                catch (System.Exception ex)
                {

                }
            }
            ADODB.Connection cn = new ADODB.Connection();
            cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath, null, null, -1);
            catalog.ActiveConnection = cn;
            ADOX.Table table = new ADOX.Table();
            table.Name = tableName;
            foreach (var column in colums)
            {
                table.Columns.Append(column);
            }
           // column.ParentCatalog = catalog;
            //column.Properties["AutoIncrement"].Value = true; //设置自动增长
            //table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null); //定义主键
            catalog.Tables.Append(table);
            cn.Close();
        }
            //========================================================================================调用
           //ADOX.Column[] columns = {
           //                     new ADOX.Column(){Name="id",Type=DataTypeEnum.adInteger,DefinedSize=9},
           //                     new ADOX.Column(){Name="col1",Type=DataTypeEnum.adWChar,DefinedSize=50},
           //                     new ADOX.Column(){Name="col2",Type=DataTypeEnum.adLongVarChar,DefinedSize=50}
           //                 };
           // AccessDbHelper.CreateAccessTable("d:\\111.mdb", "testTable", columns);
    }
}

C# 动态创建Access数据库及表

标签:

原文地址:http://www.cnblogs.com/fiskeryang/p/4185033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!