通用數據訪問層--Access版

 
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Data.OleDb;
  6. namespace GetDBMo{
  7.     class GetDbMo
  8.     {
  9.         protected static OleDbConnection conn = new OleDbConnection();
  10.         protected static OleDbCommand comm = new OleDbCommand();
  11.         private static void openConnection()
  12.         {
  13.             if (conn.State == ConnectionState.Closed)
  14.             {
  15.                 conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Settings.Default.DbPath ;//web.config文件裏設定。
  16.                 comm.Connection = conn;
  17.                 try
  18.                 {
  19.                     conn.Open();
  20.                 }
  21.                 catch (Exception e)
  22.                 { throw new Exception(e.Message); }
  23.             }
  24.         }//打開數據庫
  25.         private static void closeConnection()
  26.         {
  27.             if (conn.State == ConnectionState.Open)
  28.             {
  29.                 conn.Close();
  30.                 conn.Dispose();
  31.                 comm.Dispose();
  32.             }
  33.         }//關閉數據庫
  34.         public static void excuteSql(string sqlstr)
  35.         {
  36.             try
  37.             {
  38.                 openConnection();
  39.                 comm.CommandType = CommandType.Text;
  40.                 comm.CommandText = sqlstr;
  41.                 comm.ExecuteNonQuery();
  42.             }
  43.             catch (Exception e)
  44.             {
  45.                 throw new Exception(e.Message);
  46.             }
  47.             finally
  48.             { closeConnection(); }
  49.         }//執行sql語句
  50.         public static OleDbDataReader dataReader(string sqlstr)
  51.         {
  52.             OleDbDataReader dr = null;
  53.             try
  54.             {
  55.                 openConnection();
  56.                 comm.CommandText = sqlstr;
  57.                 comm.CommandType = CommandType.Text;
  58.                 dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
  59.             }
  60.             catch
  61.             {
  62.                 try
  63.                 {
  64.                     dr.Close();
  65.                     closeConnection();
  66.                 }
  67.                 catch { }
  68.             }
  69.             return dr;
  70.         }//返回指定sql語句的OleDbDataReader對象,使用時請注意關閉這個對象。
  71.         public static void dataReader(string sqlstr, ref OleDbDataReader dr)
  72.         {
  73.             try
  74.             {
  75.                 openConnection();
  76.                 comm.CommandText = sqlstr;
  77.                 comm.CommandType = CommandType.Text;
  78.                 dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
  79.             }
  80.             catch
  81.             {
  82.                 try
  83.                 {
  84.                     if (dr != null && !dr.IsClosed)
  85.                         dr.Close();
  86.                 }
  87.                 catch
  88.                 {
  89.                 }
  90.                 finally
  91.                 {
  92.                     closeConnection();
  93.                 }
  94.             }
  95.         }//返回指定sql語句的OleDbDataReader對象,使用時請注意關閉
  96.         public static DataSet dataSet(string sqlstr)
  97.         {
  98.             DataSet ds = new DataSet();
  99.             OleDbDataAdapter da = new OleDbDataAdapter();
  100.             try
  101.             {
  102.                 openConnection();
  103.                 comm.CommandType = CommandType.Text;
  104.                 comm.CommandText = sqlstr;
  105.                 da.SelectCommand = comm;
  106.                 da.Fill(ds);
  107.             }
  108.             catch (Exception e)
  109.             {
  110.                 throw new Exception(e.Message);
  111.             }
  112.             finally
  113.             {
  114.                 closeConnection();
  115.             }
  116.             return ds;
  117.         }//返回指定sql語句的dataset
  118.         public static void dataSet(string sqlstr, ref DataSet ds)
  119.         {
  120.             OleDbDataAdapter da = new OleDbDataAdapter();
  121.             try
  122.             {
  123.                 openConnection();
  124.                 comm.CommandType = CommandType.Text;
  125.                 comm.CommandText = sqlstr;
  126.                 da.SelectCommand = comm;
  127.                 da.Fill(ds);
  128.             }
  129.             catch (Exception e)
  130.             {
  131.                 throw new Exception(e.Message);
  132.             }
  133.             finally
  134.             {
  135.                 closeConnection();
  136.             }
  137.         }//返回指定sql語句的dataset
  138.         public static DataTable dataTable(string sqlstr)
  139.         {
  140.             DataTable dt = new DataTable();
  141.             OleDbDataAdapter da = new OleDbDataAdapter();
  142.             try
  143.             {
  144.                 openConnection();
  145.                 comm.CommandType = CommandType.Text;
  146.                 comm.CommandText = sqlstr;
  147.                 da.SelectCommand = comm;
  148.                 da.Fill(dt);
  149.             }
  150.             catch (Exception e)
  151.             {
  152.                 throw new Exception(e.Message);
  153.             }
  154.             finally
  155.             {
  156.                 closeConnection();
  157.             }
  158.             return dt;
  159.         }//返回指定sql語句的datatable
  160.         public static void dataTable(string sqlstr, ref DataTable dt)
  161.         {
  162.             OleDbDataAdapter da = new OleDbDataAdapter();
  163.             try
  164.             {
  165.                 openConnection();
  166.                 comm.CommandType = CommandType.Text;
  167.                 comm.CommandText = sqlstr;
  168.                 da.SelectCommand = comm;
  169.                 da.Fill(dt);
  170.             }
  171.             catch (Exception e)
  172.             {
  173.                 throw new Exception(e.Message);
  174.             }
  175.             finally
  176.             {
  177.                 closeConnection();
  178.             }
  179.         }//返回指定sql語句的datatable
  180.         public static DataView dataView(string sqlstr)
  181.         {
  182.             OleDbDataAdapter da = new OleDbDataAdapter();
  183.             DataView dv = new DataView();
  184.             DataSet ds = new DataSet();
  185.             try
  186.             {
  187.                 openConnection();
  188.                 comm.CommandType = CommandType.Text;
  189.                 comm.CommandText = sqlstr;
  190.                 da.SelectCommand = comm;
  191.                 da.Fill(ds);
  192.                 dv = ds.Tables[0].DefaultView;
  193.             }
  194.             catch (Exception e)
  195.             {
  196.                 throw new Exception(e.Message);
  197.             }
  198.             finally
  199.             {
  200.                 closeConnection();
  201.             }
  202.             return dv;
  203.         }
  204.         //返回指定sql語句的dataview
  205.     }
  206. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章