1. 程式人生 > >C# 操作Access的公共類

C# 操作Access的公共類

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

namespace Test
{
    /// <summary>
    /// 資料庫操作類
    /// Data 2011-4-20
    /// Author Laiminghua
    /// </summary>
    public static class DBHelp
    {
        #region 變數宣告處
        private static OleDbConnection Conn;
        private static string ConnString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"/";//連線字串
        #endregion

        #region 建構函式與連線關閉資料庫

        /// <summary>
        /// 開啟資料來源連結
        /// </summary>
        /// <returns></returns>
        public static void OpenConn()
        {
            Conn = new OleDbConnection(ConnString);
            Conn.Open();
        }

        /// <summary>
        /// 請在資料傳遞完畢後呼叫該函式,關閉資料鏈接。
        /// </summary>
        public static void CloseConn()
        {
            Conn.Close();
        }
        #endregion

        #region 資料庫基本操作
        /// <summary>
        /// 根據SQL命令返回資料DataTable資料表,
        /// 可直接作為dataGridView的資料來源
        /// </summary>
        /// <param name="SQL"></param>
        /// <returns></returns>
        public static DataTable SelectToDataTable(string SQL)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command = new OleDbCommand(SQL, Conn);
            adapter.SelectCommand = command;
            DataTable Dt = new DataTable();
            adapter.Fill(Dt);   
            return Dt;
        }

        /// <summary>
        /// 根據SQL命令返回資料DataSet資料集,其中的表可直接作為dataGridView的資料來源。
        /// </summary>
        /// <param name="SQL"></param>
        /// <param name="subtableName">在返回的資料集中所新增的表的名稱</param>
        /// <returns></returns>
        public static DataSet SelectToDataSet(string SQL, string subtableName)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command = new OleDbCommand(SQL, Conn);
            adapter.SelectCommand = command;
            DataSet Ds = new DataSet();
            Ds.Tables.Add(subtableName);
            adapter.Fill(Ds, subtableName);
            return Ds;
        }

        /// <summary>
        /// 在指定的資料集中新增帶有指定名稱的表,由於存在覆蓋已有名稱表的危險,返回操作之前的資料集。
        /// </summary>
        /// <param name="SQL"></param>
        /// <param name="subtableName">新增的表名</param>
        /// <param name="DataSetName">被新增的資料集名</param>
        /// <returns></returns>
        public static DataSet SelectToDataSet(string SQL, string subtableName, DataSet DataSetName)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command = new OleDbCommand(SQL, Conn);
            adapter.SelectCommand = command;
            DataTable Dt = new DataTable();
            DataSet Ds = new DataSet();
            Ds = DataSetName;
            adapter.Fill(DataSetName, subtableName);
            return Ds;
        }

        /// <summary>
        /// 根據SQL命令返回OleDbDataAdapter,
        /// 使用前請在主程式中新增名稱空間System.Data.OleDb
        /// </summary>
        /// <param name="SQL"></param>
        /// <returns></returns>
        public static OleDbDataAdapter SelectToOleDbDataAdapter(string SQL)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command = new OleDbCommand(SQL, Conn);
            adapter.SelectCommand = command;
            return adapter;
        }

        /// <summary>
        /// 執行刪除
        /// </summary>
        /// <param name="SQL"></param>
        /// <returns></returns>
        public static bool ExecuteSQLNonquery(string SQL)
        {
            OleDbCommand cmd = new OleDbCommand(SQL, Conn);
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
              return false;
            }
        }


        /// <summary>
        /// 執行增,改
        /// </summary>
        /// <param name="SQL"></param>
        /// <returns></returns>
        public static int ExecuteSQLScalar(string SQL)
        {
            OleDbCommand cmd = new OleDbCommand(SQL, Conn);
            try
            {
               int result= Convert.ToInt32(cmd.ExecuteScalar());
               return result;
            }
            catch
            {
                return 0;
            }
        }
        #endregion
    }
}

呼叫

//查詢
            DBHelp.OpenConn();
            DataTable dt = DBHelp.SelectToDataTable("select * from table");
            for
            DBHelp.CloseConn();

相關推薦

C# 操作Access公共

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

C# 操作INI檔案

    //補充:        //using System;     //using System.Runtime.InteropServices;     //using System.Text;     //using System.Collections;    

C#操作Ini檔案

      在Windows系統中,INI檔案是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。該檔案主要存放使用者所做的選擇以及系統的各種引數。使用者可以通過修改INI檔案,來改變應用程式和系統的很多配置。但自從Windows

C#操作Access資料庫之一(資料庫的連線)

最近使用C#對Access資料庫進行操作,在網上找了許多例程,感覺還是太雜亂,所以想著自己寫一個總結也方便大家共同學習進步。 功能:使用Access建立一個名為DatabaseStudent的資料庫,使用C#連線資料庫並利用datagridview控制元件將資料庫內容顯示出

C#操作Access時Parameters集合的使用方法

按照C#操作Sql Server資料庫的方式是不一樣的,使用時發現佔位符資料會混亂。 查閱相關資料及測試後,總結出這樣一套使用方法。 方法: public bool TsqlExecute

c#操作pdf的

1. Aspose. Pdf. Kit是個.NET 元件,用於編輯存在的PDF檔案。你可以利用它新增或移除數字簽名,加密或解密PDF檔案。它支援大量有用的功能,比如PDF圖章建立、新增CJK字型, 新增水印或Logo,解壓或新增圖片和文字,新增書籤,轉換PDF檔案為TIFF

C#操作MySQL的

and sfc comm delete class mar htable service clas C#操作MySQL的類 public class MySqlService { private static log4net.ILog

C#的access操作工具

C# access 操作工具類 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Data

ACCESS資料庫C#操作(SQLHELPER修改版)

using System;    using System.Text;    using System.Collections;    using System.Collections.Specialized;    using System.Data;    using System.Data.O

C# Directory和DirectoryInfo(文件目錄操作)

spa 當前 ext logs mat key c# static ace 對目錄操作例子: using System; using System.Collections.Generic; using System.Linq; using System.Te

C# 實現模擬登錄功能,實現公共分享。

login margin amr 獲取cookie esp zh-cn white html gda 前言 最近在研究模擬登錄的各種方法, 主要想要實現的兩個功能是: 1.點擊按鈕可以直接跳轉並登錄到某一個系統中。 2.抓取某一個系統中某一個頁面中的特定數據

C# 操作SQL Server 公共

entry def null style result cat c# pri ssa 基本夠用,存一下,以後找起來方便 public abstract class DB { //數據庫連接字符串(config來配置) prote

C#操作SqlServer MySql Oracle通用幫助

.html 結果 dbn t對象 htm cut ins 定義 div C#操作SqlServer MySql Oracle通用幫助類 【前言】   作為一款成熟的面向對象高級編程語言,C#在ADO.Net的支持上已然是做的很成熟,我們可以方便地調用ADO.Net操作各類關

C#操作CSV文件實例

thumbnail image node efault gpo padding 多線程同步 如果 str 本文實例講述了C#操作CSV文件類。分享給大家供大家參考。具體分析如下: 這個C#類用於轉換DataTable為CSV文件、CSV文件轉換成DataTable,如果需

C#操作MySql數據庫幫助(Dapper,T-Sql)

user lex object per ram int rowfilter close tex using System.Text; using MySql.Data.MySqlClient; using System.Data; using Dapper; using

C++標準庫vector型的使用和操作總結

種類 style 開始 spa log string string類 gpo targe   vector是一種類型對象的集合,它是一種順序容器,容器中的所有對象必須都是同一種類型。vector的對象是可以動態生長的,這說明它在初始化時可以不用指定大小,而是再使用時根據元素

C#操作DbCommand

異步執行 關閉 lec 繼續 關閉連接 lda () mys 成功 一、常用屬性 名稱 說明 CommandText 獲取或設置要對數據源執行的 Transact-SQL 語句、表名或存儲過程 CommandTimeout 獲取或設置在終止執行命令的嘗試並生

C 操作Redis 簡單封裝

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

C#操作MySQL

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

C 操作Redis 簡單封裝

                #include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <winsock2.h>#include <iostre