LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#連接操作Access數(shù)據(jù)庫

admin
2025年7月5日 11:22 本文熱度 154

一.前期準(zhǔn)備

數(shù)據(jù)庫 Access 2007
開發(fā)軟件 Visual Studio 2012 或者 Visual Studio 2019
C#用到DLL是 System.Data.dll

二.連接數(shù)據(jù)庫連接串

        static string exePath = System.Environment.CurrentDirectory;

        public static string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + exePath + @"\數(shù)據(jù)庫文件.mdb";

其中exePath是當(dāng)前程序的工作目錄



三.執(zhí)行查詢語句,返回DataSet

第一步創(chuàng)建數(shù)據(jù)庫連接
OleDbConnection connection = new OleDbConnection(ConnectionString)
connectionString 為連接數(shù)據(jù)庫連接串

第二步打開數(shù)據(jù)庫連接
connection.Open();

第三步執(zhí)行 查詢語句 返回 OleDbDataAdapte類
OleDbDataAdapter da = new OleDbDataAdapter(SQLString, connection);

第四步 OleDbDataAdapte填充DataSet
da.Fill(ds);

第五步 關(guān)閉連接
connection.Close();
四.執(zhí)行修改 刪除 創(chuàng)建語句,返回是否成功

第一步創(chuàng)建數(shù)據(jù)庫連接
OleDbConnection connection = new OleDbConnection(ConnectionString)
ConnectionString 為連接數(shù)據(jù)庫連接串

第二步打開數(shù)據(jù)庫連接
connection.Open();

第三步執(zhí)行 使用OleDbCommand類 執(zhí)行數(shù)據(jù)庫執(zhí)行的 SQL 語句
OleDbCommand command = new OleDbCommand(SQLString, connection);

第四步 執(zhí)行語句獲取受影響的行數(shù)
int recordCount = command.ExecuteNonQuery();
五.完整代碼

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace DBUtility
{
    public abstract class AsHelperSQL
    {
        static string exePath = System.Environment.CurrentDirectory;

        public static string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + exePath + @"\nmgzlsoft.mdb";

        #region  執(zhí)行簡單SQL語句

        /// <summary>
        /// 執(zhí)行查詢語句,返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataSet Query(string SQLString)
        {
            DataSet ds = new DataSet();

            using (OleDbConnection connection = new OleDbConnection(ConnectionString))
            {
                try
                {
                    connection.Open();
                    OleDbDataAdapter da = new OleDbDataAdapter(SQLString, connection);
                    da.Fill(ds);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
                return ds;
            }
        }

        /// <summary>
        /// 執(zhí)行sql語句返回是否成功
        /// </summary>
        /// <param name="SQLString"></param>
        /// <returns></returns>
        public static Boolean SqlAdd(string SQLString)
        {
            using (OleDbConnection connection = new OleDbConnection(ConnectionString))
            {
                try
                {
                    connection.Open();
                    OleDbCommand command = new OleDbCommand(SQLString, connection);
                    int recordCount = command.ExecuteNonQuery();

                    if (recordCount > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                catch (Exception ex)
                {
                    return false;
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        #endregion
    }
}


閱讀原文:原文鏈接


該文章在 2025/7/7 11:57:55 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲欧洲日韩一区二区日本 | 中文字幕在线不卡v一二三区 | 亚洲一区在线日韩在线尤物 | 亚洲成AV人片在线观看不卡 | 亚洲乱码精品久久久久.. | 五月丁香亚洲综合499ee |