欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 支持多類型數(shù)據(jù)庫的c#數(shù)據(jù)庫模型示例

    支持多類型數(shù)據(jù)庫的c#數(shù)據(jù)庫模型示例

    本文為大家提供一個c#數(shù)據(jù)庫訪問模型,支持多類型數(shù)據(jù)庫,簡單抽取數(shù)據(jù)庫訪問函數(shù),大家參考使用吧
    2014-01-01
  • C#生成隨機字符串的實例

    C#生成隨機字符串的實例

    本文介紹了“C#生成隨機字符串的實例”,需要的朋友可以參考一下
    2013-03-03
  • C#創(chuàng)建不規(guī)則窗體的4種方式詳解

    C#創(chuàng)建不規(guī)則窗體的4種方式詳解

    在這里我們將實現(xiàn)的是C#創(chuàng)建不規(guī)則窗體的幾種方式,包括自定義窗體,不規(guī)則圖形等等。希望對大家有所幫助。
    2015-10-10
  • 深入了解C#多線程安全

    深入了解C#多線程安全

    使用多線程無法避免的一個問題就是多線程安全。那什么是多線程安全?如何解決多線程安全?本文將通過一些簡單的例子為大家詳細介紹一下多線程相關(guān)的問題,感興趣的可以了解一下
    2021-12-12
  • asp.net之生成驗證碼的方法集錦(一)

    asp.net之生成驗證碼的方法集錦(一)

    現(xiàn)在很多網(wǎng)站都有注冊登錄的頁面,為了更好的滿足用戶體驗和網(wǎng)站的安全性,很多網(wǎng)站都采用動態(tài)生成的圖形碼或者是附加碼進行驗證,這篇文章主要就是介紹生成驗證碼的方法,需要的朋友可以參考下
    2015-08-08
  • C#自定義特性(Attribute)詳解

    C#自定義特性(Attribute)詳解

    本文詳細講解了C#的自定義特性(Attribute),文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#實現(xiàn)的一款比較美觀的驗證碼完整實例

    C#實現(xiàn)的一款比較美觀的驗證碼完整實例

    這篇文章主要介紹了C#實現(xiàn)的一款比較美觀的驗證碼,以完整實例形式分析了C#生成驗證碼與前端調(diào)用驗證碼的實現(xiàn)技巧,需要的朋友可以參考下
    2016-06-06
  • winform dateTime數(shù)據(jù)類型轉(zhuǎn)換方法

    winform dateTime數(shù)據(jù)類型轉(zhuǎn)換方法

    這篇文章主要介紹了winform dateTime數(shù)據(jù)類型轉(zhuǎn)換方法,需要的朋友可以參考下
    2017-02-02
  • C#操作INI文件的示例代碼

    C#操作INI文件的示例代碼

    這篇文章主要為大家詳細介紹了C#操作INI文件的相關(guān)知識,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨上班一起學(xué)習(xí)一下
    2023-12-12
  • C#編程實現(xiàn)動態(tài)改變配置文件信息的方法

    C#編程實現(xiàn)動態(tài)改變配置文件信息的方法

    這篇文章主要介紹了C#編程實現(xiàn)動態(tài)改變配置文件信息的方法,涉及C#針對xml格式文件的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06

最新評論