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

C#數(shù)據(jù)庫操作類AccessHelper實例

 更新時間:2014年10月14日 15:23:11   投稿:shichen2014  
這篇文章主要介紹了C#數(shù)據(jù)庫操作類AccessHelper實例,可實現(xiàn)針對access數(shù)據(jù)庫的各種常見操作,非常具有實用價值,需要的朋友可以參考下

本文實例講述了C#數(shù)據(jù)庫操作類AccessHelper。分享給大家供大家參考。

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

復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using ahwildlife.Utils;


/// <summary>
/// AccessHelper 的摘要說明
/// </summary>
public class AccessHelper
{
    #region 變量
    protected static OleDbConnection conn = new OleDbConnection();
    protected static OleDbCommand comm = new OleDbCommand();
    protected static string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ahwildlife.mdb;Persist Security Info=False;Jet OLEDB:Database Password=sa;";
    #endregion

    #region 構(gòu)造函數(shù)
    /// <summary>
    /// 構(gòu)造函數(shù)
    /// </summary>
    public AccessHelper()
    {

    }
    #endregion

    #region 打開數(shù)據(jù)庫
    /// <summary>
    /// 打開數(shù)據(jù)庫
    /// </summary>
    private static void openConnection()
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ahwildlife.mdb;Persist Security Info=False;Jet OLEDB:Database Password=sa;";
            comm.Connection = conn;
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
    #endregion

    #region 關(guān)閉數(shù)據(jù)庫
    /// <summary>
    /// 關(guān)閉數(shù)據(jù)庫
    /// </summary>
    private static void closeConnection()
    {
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
    }
    #endregion

    #region 執(zhí)行sql語句
    /// <summary>
    /// 執(zhí)行sql語句
    /// </summary>
    public static void ExecuteSql(string sqlstr)
    {
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            comm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            closeConnection();
        }
    }
    #endregion

    #region 返回指定sql語句的OleDbDataReader對象,使用時請注意關(guān)閉這個對象。
    /// <summary>
    /// 返回指定sql語句的OleDbDataReader對象,使用時請注意關(guān)閉這個對象。
    /// </summary>
    public static OleDbDataReader DataReader(string sqlstr)
    {
        OleDbDataReader dr = null;
        try
        {
            openConnection();
            comm.CommandText = sqlstr;
            comm.CommandType = CommandType.Text;

            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch
        {
            try
            {
                dr.Close();
                closeConnection();
            }
            catch { }
        }
        return dr;
    }
    #endregion

    #region 返回指定sql語句的OleDbDataReader對象,使用時請注意關(guān)閉
    /// <summary>
    /// 返回指定sql語句的OleDbDataReader對象,使用時請注意關(guān)閉
    /// </summary>
    public static void DataReader(string sqlstr, ref OleDbDataReader dr)
    {
        try
        {
            openConnection();
            comm.CommandText = sqlstr;
            comm.CommandType = CommandType.Text;
            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch
        {
            try
            {
                if (dr != null && !dr.IsClosed)
                    dr.Close();
            }
            catch
            {
            }
            finally
            {
                closeConnection();
            }
        }
    }
    #endregion

    #region 返回指定sql語句的DataSet
    /// <summary>
    /// 返回指定sql語句的DataSet
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    public static DataSet DataSet(string sqlstr)
    {
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);

        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return ds;
    }
    #endregion

    #region 返回指定sql語句的DataSet
    /// <summary>
    /// 返回指定sql語句的DataSet
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="ds"></param>
    public static void DataSet(string sqlstr, ref DataSet ds)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
    }
    #endregion

    #region 返回指定sql語句的DataTable
    /// <summary>
    /// 返回指定sql語句的DataTable
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    public static DataTable DataTable(string sqlstr)
    {
        DataTable dt = Common.GetDataTableCache(sqlstr);//讀緩存
        if (dt != null)
        {
            return dt.Copy();
        }
        else
        {
            dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter();
            try
            {
                using (OleDbConnection conn = new OleDbConnection())
                {
                    conn.ConnectionString = connectionString;
                    conn.Open();
                    using (OleDbCommand comm = new OleDbCommand())
                    {
                        comm.Connection = conn;
                        comm.CommandType = CommandType.Text;
                        comm.CommandText = sqlstr;
                        da.SelectCommand = comm;
                        da.Fill(dt);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                closeConnection();
            }
            Common.InsertDataTableCache(sqlstr, dt);//添加緩存
            return dt.Copy();
        }
    }
    #endregion

    #region 返回指定sql語句的DataTable
    /// <summary>
    /// 返回指定sql語句的DataTable
    /// </summary>
    public static void DataTable(string sqlstr, ref DataTable dt)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(dt);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
    }
    #endregion

    #region 返回指定sql語句的DataView
    /// <summary>
    /// 返回指定sql語句的DataView
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    public static DataView DataView(string sqlstr)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataView dv = new DataView();
        DataSet ds = new DataSet();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
            dv = ds.Tables[0].DefaultView;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return dv;
    }
    #endregion
}

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

  • C#實現(xiàn)將一個字符串進(jìn)行翻轉(zhuǎn)顯示的6種方法

    C#實現(xiàn)將一個字符串進(jìn)行翻轉(zhuǎn)顯示的6種方法

    下面小編就為大家分享一篇C#實現(xiàn)將一個字符串進(jìn)行翻轉(zhuǎn)顯示的6種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#讀取CSV文件的方法總結(jié)

    C#讀取CSV文件的方法總結(jié)

    CSV文件是一種簡單的文本文件格式,用于存儲表格數(shù)據(jù),在C#中,有多種方法可以用于讀取CSV文件,本文將介紹幾種常見的讀取CSV文件的方法,包括使用System.IO命名空間中的類、使用CsvHelper庫以及使用LINQ,需要的朋友可以參考下
    2024-05-05
  • C#實現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法

    C#實現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法

    這篇文章主要介紹了C#實現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法,涉及C#網(wǎng)頁瀏覽器及圖片操作的相關(guān)技巧,需要的朋友可以參考下
    2016-01-01
  • WPF如何實現(xiàn)日期范圍選擇器

    WPF如何實現(xiàn)日期范圍選擇器

    這篇文章主要為大家詳細(xì)介紹了WPF如何實現(xiàn)日期范圍選擇器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • C#規(guī)則引擎RulesEngine的具體使用

    C#規(guī)則引擎RulesEngine的具體使用

    這篇文章主要介紹了C#規(guī)則引擎RulesEngine的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • C#函數(shù)式編程中的惰性求值詳解

    C#函數(shù)式編程中的惰性求值詳解

    這篇文章主要介紹了C#函數(shù)式編程中的惰性求值詳解,本文講解了惰性求值的相關(guān)知識并給出代碼實例,需要的朋友可以參考下
    2015-01-01
  • C#中ftp檢測目錄是否存在和創(chuàng)建文件夾的實現(xiàn)

    C#中ftp檢測目錄是否存在和創(chuàng)建文件夾的實現(xiàn)

    本文主要介紹了C#中ftp檢測目錄是否存在和創(chuàng)建文件夾的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C#中Action和Func的區(qū)別

    C#中Action和Func的區(qū)別

    這篇文章主要介紹了C#中Action和Func的區(qū)別,是進(jìn)行C#程序設(shè)計時需要加以注意的知識點,需要的朋友可以參考下
    2014-09-09
  • C#實現(xiàn)仿QQ抽屜式窗體的設(shè)計方法

    C#實現(xiàn)仿QQ抽屜式窗體的設(shè)計方法

    QQ軟件對于絕大多數(shù)的人來說再熟悉不過了,它以使用方便、界面美觀及功能完善而著稱,本文給大家介紹了C#實現(xiàn)仿QQ抽屜式窗體的設(shè)計方法,主要通過使用API函數(shù)WindowFromPoint和GetParent實現(xiàn)仿QQ的抽屜式窗體,需要的朋友可以參考下
    2024-04-04
  • C#實現(xiàn)ComboBox自動匹配字符

    C#實現(xiàn)ComboBox自動匹配字符

    本文介紹C#如何實現(xiàn)ComboBox自動匹配字符1.采用CustomSource當(dāng)做提示集合2. 直接使用下拉列表中的項作為匹配的集合,需要了解的朋友可以參考下
    2012-12-12

最新評論

涿州市| 临城县| 徐闻县| 贡嘎县| 凭祥市| 荔浦县| 汉源县| 都匀市| 岑巩县| 镇康县| 镇远县| 岳阳市| 烟台市| 苏州市| 正镶白旗| 上杭县| 大石桥市| 辽阳市| 台湾省| 张家口市| 皮山县| 九龙坡区| 河南省| 枣强县| 宝鸡市| 扎囊县| 荣成市| 全椒县| 措勤县| 宁国市| 金塔县| 正阳县| 米泉市| 泰和县| 东方市| 邯郸县| 濮阳县| 康定县| 稷山县| 孟州市| 绥阳县|