国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作

這篇具有很好參考價(jià)值的文章主要介紹了C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、實(shí)現(xiàn)效果

1.1、實(shí)現(xiàn)功能

? ①實(shí)現(xiàn)創(chuàng)建Access數(shù)據(jù)庫(kù);

? ②實(shí)現(xiàn)創(chuàng)建指定Access數(shù)據(jù)庫(kù)的表;

? ③實(shí)現(xiàn)給Access數(shù)據(jù)庫(kù)的指定表【插入、查詢(xún)、更新、刪除、分頁(yè)查詢(xún)】數(shù)據(jù);

? ④實(shí)現(xiàn)獲取Access數(shù)據(jù)庫(kù)中的所有表名稱(chēng)及其表包含的所有列名稱(chēng)

1.2、效果圖

C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作

二、實(shí)現(xiàn)核心

該項(xiàng)目的完整工程下載地址如下:Access數(shù)據(jù)庫(kù)操作項(xiàng)目的完整工程下載地址https://download.csdn.net/download/xiaochenXIHUA/85163940

2.1、添加引用

? 在項(xiàng)目中添加【Microsoft ActiveX Data Objects 6.0 Library】和【Microsoft ADO Ext.6.0 for DDL and Security】引用,如下圖所示。

C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作

C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作

注意事項(xiàng):

?引入這兩個(gè)Com組件后,在使用【ADOX.CatalogClass】報(bào)如下圖的錯(cuò)誤時(shí);只用選中【Interop.ADOX】,然后在屬性下的【嵌入互操作類(lèi)型-->修改為否】即可解決,如下圖所示:

C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作

C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作

?C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作

2.2、Access數(shù)據(jù)幫助類(lèi)

這里Access數(shù)據(jù)庫(kù)幫助類(lèi)的部分內(nèi)容,如下所示:

/***
*	Title:"輕量數(shù)據(jù)庫(kù)" 項(xiàng)目
*		主題:Access數(shù)據(jù)庫(kù)的幫助類(lèi)
*	Description:
*		功能:
*		    ①構(gòu)造函數(shù)時(shí)可以創(chuàng)建Access指定的連接字符串
*		    ②創(chuàng)建Access的mdb類(lèi)型數(shù)據(jù)庫(kù)
*	Date:2022
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using LiteDBHelper.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Text;

namespace LiteDBHelper
{
    public class AccessDBHelper
    {

        #region   基礎(chǔ)參數(shù)
        //數(shù)據(jù)庫(kù)連接字符串
        private string _ConnStr;

        //獲取到數(shù)據(jù)庫(kù)連接字符串
        public string ConnStr { get { return _ConnStr; } }

        #endregion 


        #region   構(gòu)造函數(shù)
        /// <summary>
        /// mdb文件的連接字符串構(gòu)造函數(shù)
        /// </summary>
        /// <param name="connnection"></param>
        public AccessDBHelper(string connnection)
        {
            if (string.IsNullOrEmpty(connnection)) return;

            _ConnStr = connnection;
        }

        /// <summary>
        /// mdb文件無(wú)用戶名和密碼構(gòu)造函數(shù)
        /// </summary>
        /// <param name="mdbFilePathAndName">mdb文件的路徑和名稱(chēng)(比如:@"D:\\HalmEL\\2022-4-11.mdb")</param>
        public AccessDBHelper(string mdbFilePathAndName,AccessDBType accessDBType)
        {
            if (string.IsNullOrEmpty(mdbFilePathAndName)) return;

            string strDriver = GetDirverOfAccessDBType(accessDBType);
            _ConnStr = $"{strDriver};Data source={mdbFilePathAndName};";

            InstanceSqlHelper(_ConnStr);
        }

        #endregion


        #region   創(chuàng)建Access數(shù)據(jù)庫(kù)、表及其字段

        /// <summary>
        /// 創(chuàng)建Mdb數(shù)據(jù)庫(kù)
        /// </summary>
        /// <param name="mdbFilePathAndName">mdb文件的路徑和名稱(chēng)(比如:@"D:\\HalmEL\\2022-4-11.mdb")</param>
        /// <returns>返回創(chuàng)建結(jié)果</returns>
        public ResultInfo CreateMdbDataBase(string mdbFilePathAndName)
        {
            ResultInfo resultInfo = new ResultInfo();

            if (File.Exists(mdbFilePathAndName))
            {
                resultInfo.ResultStatus = ResultStatus.Success;
                resultInfo.Message = $"{mdbFilePathAndName} 文件已經(jīng)存在!";
            }
            try
            {
                //如果目錄不存在,則創(chuàng)建目錄
                string folder = Path.GetDirectoryName(mdbFilePathAndName);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                //創(chuàng)建Catalog目錄類(lèi)
                ADOX.CatalogClass catalog = new ADOX.CatalogClass();

                //根據(jù)聯(lián)結(jié)字符串使用Jet數(shù)據(jù)庫(kù)引擎創(chuàng)建數(shù)據(jù)庫(kù)
                catalog.Create(_ConnStr);
                catalog = null;

                resultInfo.ResultStatus = ResultStatus.Success;
                resultInfo.Message = $"{mdbFilePathAndName} 文件創(chuàng)建成功!";
            }
            catch (Exception ex)
            {
                resultInfo.ResultStatus = ResultStatus.Error;
                resultInfo.Message = $"{ex.Message}";
            }

            return resultInfo;
        }

        /// <summary>
        /// 創(chuàng)建mdb表(字段都是短文本類(lèi)型)
        /// </summary>
        /// <param name="mdbFilePathAndName">mdb文件的路徑和名稱(chēng)(比如:@"D:\\HalmEL\\2022-4-11.mdb")</param>
        /// <param name="tableName">表名稱(chēng)</param>
        /// <param name="fieldNameList">表字段名稱(chēng)列表</param>
        /// <returns></returns>
        public ResultInfo CreateMdbTable(string mdbFilePathAndName, string tableName, List<string> fieldNameList)
        {
            ResultInfo resultInfo = new ResultInfo();

            if (string.IsNullOrEmpty(mdbFilePathAndName) || string.IsNullOrEmpty(tableName)
                || fieldNameList == null || fieldNameList.Count < 1)
            {
                resultInfo.SetContent(ResultStatus.Error, "內(nèi)容為空,請(qǐng)檢查!", null);

                return resultInfo;
            }


            ADOX.CatalogClass catalog = new ADOX.CatalogClass();
            ADODB.Connection connection = new ADODB.Connection();

            try
            {
                //打開(kāi)數(shù)據(jù)庫(kù)連接
                connection.Open(_ConnStr, null, null, -1);
                catalog.ActiveConnection = connection;

                //新建一個(gè)表
                ADOX.TableClass table = new ADOX.TableClass();
                table.ParentCatalog = catalog;
                table.Name = tableName;

                int fieldCount = fieldNameList.Count;
                for (int i = 0; i < fieldCount; i++)
                {
                    //增加一個(gè)文本字段
                    string fieldName = fieldNameList[i].ToString();
                    ADOX.ColumnClass column = new ADOX.ColumnClass();
                    column.ParentCatalog = catalog;
                    column.Name = fieldName;
                    column.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
                    //table.Columns.Append(column, ADOX.DataTypeEnum.adLongVarChar, 100);
                    table.Columns.Append(fieldName, ADOX.DataTypeEnum.adVarWChar, 106);

                }

                //將創(chuàng)建的表加入數(shù)據(jù)庫(kù)
                catalog.Tables.Append(table);
                table = null;
                catalog = null;

                resultInfo.SetContent(ResultStatus.Success, $"創(chuàng)建:{tableName} 表成功", null);
            }
            catch (Exception ex)
            {
                resultInfo.SetContent(ResultStatus.Error, $"{ex.Message}", null);
            }
            finally
            {
                //關(guān)閉連接
                connection.Close();
            }

            return resultInfo;
        }

        #endregion



        #region   私有方法

        /// <summary>
        /// 根據(jù)Access類(lèi)型返回對(duì)應(yīng)的驅(qū)動(dòng)
        /// </summary>
        /// <param name="accessDBType">Access數(shù)據(jù)庫(kù)類(lèi)型</param>
        /// <returns></returns>
        private string GetDirverOfAccessDBType(AccessDBType accessDBType)
        {
            string connStr = $"Microsoft.ACE.OLEDB.12.0";

            switch (accessDBType)
            {
                case AccessDBType.Is2007AndLater:
                    connStr = $"Provider=Microsoft.ACE.OLEDB.12.0";
                    break;
                case AccessDBType.Is2003AndBefore:
                    connStr = $"Provider=Microsoft.Jet.OLEDB.4.0";
                    break;
                default:
                    break;
            }

            return connStr;
        }

        #endregion 


    }//Class_end


    /// <summary>
    /// Access數(shù)據(jù)庫(kù)類(lèi)型
    /// </summary>
    public enum AccessDBType
    {
        //2007及其更高的版本
        Is2007AndLater,
        //2003等之前的版本
        Is2003AndBefore,

    }


}

2.3、關(guān)于未注冊(cè)Microsoft.ACE.OLEDB.12.0解決辦法

程序報(bào)錯(cuò)“未在本地計(jì)算機(jī)上注冊(cè)“Microsoft.ACE.OLEDB.12.0”提供程序”解決辦法https://blog.csdn.net/xiaochenXIHUA/article/details/124031921?spm=1001.2014.3001.5501文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-481881.html

三、其他的相關(guān)資料

DataTypeEnum - ActiveX Data Objects (ADO) | Microsoft Docshttps://docs.microsoft.com/zh-cn/sql/ado/reference/ado-api/datatypeenum?view=sql-server-ver15Access 桌面數(shù)據(jù)庫(kù)的數(shù)據(jù)類(lèi)型 (microsoft.com)https://support.microsoft.com/zh-cn/office/access-%E6%A1%8C%E9%9D%A2%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B-df2b83ba-cef6-436d-b679-3418f622e482#ID0EBBD=Office_2007_-_2010INSERT INTO 語(yǔ)句 (Microsoft Access SQL) | Microsoft Docshttps://docs.microsoft.com/zh-cn/office/client-developer/access/desktop-database-reference/insert-into-statement-microsoft-access-sql概念(Access VBA 參考) | Microsoft Docshttps://docs.microsoft.com/zh-cn/office/vba/access/concepts/miscellaneous/concepts-access-vba-reference\t \r \n轉(zhuǎn)義字符https://www.cnblogs.com/lsqbk/p/10259044.html

到了這里,關(guān)于C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • C語(yǔ)言通過(guò)ODBC函數(shù)操作Access數(shù)據(jù)庫(kù)(mdb和accdb格式)(char字符數(shù)組)

    C語(yǔ)言通過(guò)ODBC函數(shù)操作Access數(shù)據(jù)庫(kù)(mdb和accdb格式)(char字符數(shù)組)

    編譯環(huán)境:Windows XP + Visual Studio 2010 數(shù)據(jù)庫(kù):Access 2010,accdb格式 本例程只使用char[]字符數(shù)組,不使用wchar_t[]字符數(shù)組,更適合C語(yǔ)言初學(xué)者。 如果讀取字符串時(shí),db_bind_str提供的字符數(shù)組空間小了,db_fetch會(huì)執(zhí)行失敗返回-2。 由于Windows系統(tǒng)設(shè)計(jì)原因,char[]字符數(shù)組只能存儲(chǔ)G

    2024年02月02日
    瀏覽(25)
  • C# Dapper 操作Oracle數(shù)據(jù)庫(kù)

    C# Dapper 操作Oracle數(shù)據(jù)庫(kù)

    nuget安裝內(nèi)容? ?1.配置連接字符串?OracleConnectionString這個(gè)可用 ?2.讀取配置文件類(lèi) 3.Dapper數(shù)據(jù)庫(kù)操作類(lèi)? 4.操作數(shù)據(jù)實(shí)例?

    2024年02月10日
    瀏覽(21)
  • C#操作MSSQL數(shù)據(jù)庫(kù) -增刪改查

    要在C#中連接到Microsoft SQL Server數(shù)據(jù)庫(kù)(MSSQL),你可以使用.NET Framework提供的System.Data.SqlClient命名空間中的類(lèi)。 以下是一個(gè)簡(jiǎn)單的示例代碼,展示了如何在C#中使用MSSQL數(shù)據(jù)庫(kù)鏈接: 在上述代碼中,你需要替換 serverName 、 databaseName 、 userName 和 password 為你實(shí)際的數(shù)據(jù)庫(kù)服務(wù)器

    2024年02月10日
    瀏覽(22)
  • ES 實(shí)現(xiàn)數(shù)據(jù)庫(kù)or查詢(xún)效果

    ES :有兩種常用查詢(xún)?? ????????must? ?必須滿足查詢(xún)條件 ????????should 非必須滿足查詢(xún)條件 ?如果想實(shí)現(xiàn)類(lèi)似與mysql中的or 查詢(xún)效果,必須使用should查詢(xún)。但是shuold 會(huì)查詢(xún)出不滿足條件的數(shù)據(jù) ,這必須加一個(gè)屬性?\\\"minimum_should_match\\\": \\\"1\\\" 必須滿足should條件中的一個(gè)查詢(xún)

    2024年02月11日
    瀏覽(21)
  • 實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查+界面效果-----jsp

    實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查+界面效果-----jsp

    1.通過(guò)連接數(shù)據(jù)庫(kù)完成用戶登錄模塊。 2.登錄成功后查詢(xún)出一張數(shù)據(jù)庫(kù)表中的內(nèi)容;登錄不成功返回登錄頁(yè)面。 3.頁(yè)面面端要有空值和非法字符驗(yàn)證。 4.登錄成功后對(duì)一張表中數(shù)據(jù)進(jìn)行增加、刪除、修改和查詢(xún)操作。 創(chuàng)建數(shù)據(jù)庫(kù) 名字為jdbcHomework,字符編碼為utf8 數(shù)據(jù)庫(kù)創(chuàng)建表

    2024年02月06日
    瀏覽(18)
  • 【C# .NET 】使用 Entity Framework Core 操作sqlite數(shù)據(jù)庫(kù)

    添加包 EF Core design package? ?NuGet Gallery | Home 使用用于?EF Core 遷移和現(xiàn)有數(shù)據(jù)庫(kù)中的反向工程(基架)的工具需要安裝相應(yīng)的工具包: 可在 Visual Studio?包管理器控制臺(tái)中使用的 PowerShell 工具的?Microsoft.EntityFrameworkCore.Tools 跨平臺(tái)命令行工具的?dotnet-ef?和?Microsoft.EntityFramewor

    2024年02月14日
    瀏覽(25)
  • MySQL 5.7詳細(xì)下載安裝配置以及C# MySQL數(shù)據(jù)庫(kù)操作教程

    MySQL 5.7詳細(xì)下載安裝配置以及C# MySQL數(shù)據(jù)庫(kù)操作教程

    MySQL 5.7詳細(xì)下載安裝配置以及C# MySQL數(shù)據(jù)庫(kù)操作教程 最近有個(gè)項(xiàng)目使用MySQL5.7,在安裝MySQL的時(shí)候會(huì)遇到很多問(wèn)題,博客上其實(shí)也有很多解決問(wèn)題的辦法,在這里我操作記錄一下,方便后續(xù)使用時(shí)查看。 以下MySQL 5.7.43 詳細(xì)下載安裝配置教程。 訪問(wèn)官方網(wǎng)站:https://www.mysql.co

    2024年02月09日
    瀏覽(22)
  • PG數(shù)據(jù)庫(kù)實(shí)現(xiàn)高可用方案(包括通用型方案Corosync+pacemaker協(xié)作)

    PG數(shù)據(jù)庫(kù)實(shí)現(xiàn)高可用方案(包括通用型方案Corosync+pacemaker協(xié)作)

    系列文章 keepalived學(xué)習(xí)記錄:對(duì)其vip漂移過(guò)程采用gdb跟蹤 Keepalived與HaProxy的協(xié)調(diào)合作原理分析 Oracle實(shí)現(xiàn)高可用性的工具(負(fù)載均衡/故障切換) 達(dá)夢(mèng)實(shí)現(xiàn)高可用性的實(shí)現(xiàn)(failover功能/負(fù)載均衡/虛擬ip透明切換) PG數(shù)據(jù)庫(kù)實(shí)現(xiàn)高可用方案(包括通用型方案Corosync+pacemaker協(xié)作) 在

    2024年02月06日
    瀏覽(17)
  • C#實(shí)現(xiàn)SqlServer數(shù)據(jù)庫(kù)同步

    C#實(shí)現(xiàn)SqlServer數(shù)據(jù)庫(kù)同步

    實(shí)現(xiàn)效果: 設(shè)計(jì)思路: 1. 開(kāi)啟數(shù)據(jù)庫(kù)及表的cdc,定時(shí)查詢(xún)cdc表數(shù)據(jù),封裝sql語(yǔ)句(通過(guò)執(zhí)行類(lèi)型,主鍵;修改類(lèi)型的cdc數(shù)據(jù)只取最后更新的記錄),添加到離線數(shù)據(jù)表; 2. 線程定時(shí)查詢(xún)離線數(shù)據(jù)表,更新遠(yuǎn)程庫(kù)數(shù)據(jù); 3. 遠(yuǎn)程庫(kù)數(shù)據(jù)被更改又會(huì)產(chǎn)生cdc數(shù)據(jù),對(duì)此數(shù)據(jù)進(jìn)行攔截;

    2024年02月13日
    瀏覽(22)
  • C#調(diào)用SqlSugar操作達(dá)夢(mèng)數(shù)據(jù)庫(kù)報(bào)錯(cuò)“無(wú)效的表或視圖名”

    C#調(diào)用SqlSugar操作達(dá)夢(mèng)數(shù)據(jù)庫(kù)報(bào)錯(cuò)“無(wú)效的表或視圖名”

    ??安裝達(dá)夢(mèng)數(shù)據(jù)庫(kù)后,使用SqlSugar連接測(cè)試數(shù)據(jù)庫(kù)并基于DBFirst方式創(chuàng)建數(shù)據(jù)庫(kù)表對(duì)應(yīng)的類(lèi),主要代碼如下: ??運(yùn)行到CreateClassFile函數(shù)時(shí)報(bào)如下錯(cuò)誤: ??通過(guò)達(dá)夢(mèng)管理工具查看數(shù)據(jù)庫(kù),PERSON數(shù)據(jù)庫(kù)下有ADDRESS表,不清楚為什么報(bào)錯(cuò)。 ??百度錯(cuò)誤信息,檢索結(jié)果中介紹可

    2024年01月25日
    瀏覽(22)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包