C#數(shù)據(jù)庫(kù)操作類(lèi)AccessHelper實(shí)例
本文實(shí)例講述了C#數(shù)據(jù)庫(kù)操作類(lèi)AccessHelper。分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using ahwildlife.Utils;
/// <summary>
/// AccessHelper 的摘要說(shuō)明
/// </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 打開(kāi)數(shù)據(jù)庫(kù)
/// <summary>
/// 打開(kāi)數(shù)據(jù)庫(kù)
/// </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ù)庫(kù)
/// <summary>
/// 關(guān)閉數(shù)據(jù)庫(kù)
/// </summary>
private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
comm.Dispose();
}
}
#endregion
#region 執(zhí)行sql語(yǔ)句
/// <summary>
/// 執(zhí)行sql語(yǔ)句
/// </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語(yǔ)句的OleDbDataReader對(duì)象,使用時(shí)請(qǐng)注意關(guān)閉這個(gè)對(duì)象。
/// <summary>
/// 返回指定sql語(yǔ)句的OleDbDataReader對(duì)象,使用時(shí)請(qǐng)注意關(guān)閉這個(gè)對(duì)象。
/// </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語(yǔ)句的OleDbDataReader對(duì)象,使用時(shí)請(qǐng)注意關(guān)閉
/// <summary>
/// 返回指定sql語(yǔ)句的OleDbDataReader對(duì)象,使用時(shí)請(qǐng)注意關(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語(yǔ)句的DataSet
/// <summary>
/// 返回指定sql語(yǔ)句的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語(yǔ)句的DataSet
/// <summary>
/// 返回指定sql語(yǔ)句的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語(yǔ)句的DataTable
/// <summary>
/// 返回指定sql語(yǔ)句的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語(yǔ)句的DataTable
/// <summary>
/// 返回指定sql語(yǔ)句的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語(yǔ)句的DataView
/// <summary>
/// 返回指定sql語(yǔ)句的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
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#編程實(shí)現(xiàn)連接ACCESS數(shù)據(jù)庫(kù)實(shí)例詳解
- C#使用ADO.Net部件來(lái)訪問(wèn)Access數(shù)據(jù)庫(kù)的方法
- C#通過(guò)oledb訪問(wèn)access數(shù)據(jù)庫(kù)的方法
- C#動(dòng)態(tài)創(chuàng)建Access數(shù)據(jù)庫(kù)及表的方法
- c#連接access數(shù)據(jù)庫(kù)操作類(lèi)分享
- C# Access數(shù)據(jù)庫(kù)增刪查改的簡(jiǎn)單方法
- c# 連接access數(shù)據(jù)庫(kù)config配置
- 利用C#遠(yuǎn)程存取Access數(shù)據(jù)庫(kù)
- C# 操作 access 數(shù)據(jù)庫(kù)的實(shí)例代碼
相關(guān)文章
C#實(shí)現(xiàn)將一個(gè)字符串進(jìn)行翻轉(zhuǎn)顯示的6種方法
下面小編就為大家分享一篇C#實(shí)現(xiàn)將一個(gè)字符串進(jìn)行翻轉(zhuǎn)顯示的6種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
C#實(shí)現(xiàn)在網(wǎng)頁(yè)中根據(jù)url截圖并輸出到網(wǎng)頁(yè)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在網(wǎng)頁(yè)中根據(jù)url截圖并輸出到網(wǎng)頁(yè)的方法,涉及C#網(wǎng)頁(yè)瀏覽器及圖片操作的相關(guān)技巧,需要的朋友可以參考下2016-01-01
C#中ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn)
本文主要介紹了C#中ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C#實(shí)現(xiàn)仿QQ抽屜式窗體的設(shè)計(jì)方法
QQ軟件對(duì)于絕大多數(shù)的人來(lái)說(shuō)再熟悉不過(guò)了,它以使用方便、界面美觀及功能完善而著稱,本文給大家介紹了C#實(shí)現(xiàn)仿QQ抽屜式窗體的設(shè)計(jì)方法,主要通過(guò)使用API函數(shù)WindowFromPoint和GetParent實(shí)現(xiàn)仿QQ的抽屜式窗體,需要的朋友可以參考下2024-04-04
C#實(shí)現(xiàn)ComboBox自動(dòng)匹配字符
本文介紹C#如何實(shí)現(xiàn)ComboBox自動(dòng)匹配字符1.采用CustomSource當(dāng)做提示集合2. 直接使用下拉列表中的項(xiàng)作為匹配的集合,需要了解的朋友可以參考下2012-12-12

