最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法

 更新時間:2014年12月26日 09:41:32   投稿:shichen2014  
這篇文章主要介紹了C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法,以實例形式分析了創(chuàng)建access數(shù)據(jù)庫及在access數(shù)據(jù)庫中建表的完整過程,是非常實用的技巧,需要的朋友可以參考下

本文實例講述了C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法。分享給大家供大家參考。

具體實現(xiàn)方法如下:

復制代碼 代碼如下:

//添加兩個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>
        /// 創(chuàng)建access數(shù)據(jù)庫
        /// </summary>
        /// <param name="filePath">數(shù)據(jù)庫文件的全路徑,如 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數(shù)據(jù)庫中創(chuàng)建表
        /// </summary>
        /// <param name="filePath">數(shù)據(jù)庫表文件全路徑如D:\\NewDb.mdb 沒有則創(chuàng)建 </param>
        /// <param name="tableName">表名</param>
        /// <param name="colums">ADOX.Column對象數(shù)組</param>
        public static void CreateAccessTable(string filePath, string tableName, params ADOX.Column[] colums)
        {
            ADOX.Catalog catalog = new Catalog();
            //數(shù)據(jù)庫文件不存在則創(chuàng)建
            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#程序設計有所幫助。

相關文章

最新評論

商水县| 铁岭市| 伽师县| 德江县| 商河县| 镇坪县| 临湘市| 华阴市| 锦州市| 福建省| 东阿县| 黄冈市| 滁州市| 绩溪县| 南开区| 隆尧县| 万全县| 贡觉县| 柳州市| 闽侯县| 若羌县| 陆川县| 红桥区| 英德市| 沙河市| 长沙县| 乌鲁木齐县| 三河市| 儋州市| 监利县| 西乡县| 佛冈县| 新源县| 清水河县| 云林县| 武宣县| 灵丘县| 皮山县| 宣城市| 天水市| 胶南市|