C# 动态创建Access数据库及表_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# 动态创建Access数据库及表

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

 2014/12/25 18:05:50  fiskeryang  程序员俱乐部  我要评论(0)
  • 摘要://添加两个com组件引用//MicrosoftADOExt.2.8forDDLandSecurity//MicrosoftActiveXDataObjects2.8LibraryusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingADOX;usingSystem.IO;namespaceWebRequestTest.Common
  • 标签:C# Access 创建 数据库 数据 ACCESS数据库 access数据
//添加两个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);
    }
}

发表评论
用户名: 匿名