- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Data.OleDb;
- namespace GetDBMo{
- class GetDbMo
- {
- protected static OleDbConnection conn = new OleDbConnection();
- protected static OleDbCommand comm = new OleDbCommand();
- private static void openConnection()
- {
- if (conn.State == ConnectionState.Closed)
- {
- conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Settings.Default.DbPath ;//web.config文件裏設定。
- comm.Connection = conn;
- try
- {
- conn.Open();
- }
- catch (Exception e)
- { throw new Exception(e.Message); }
- }
- }//打開數據庫
- private static void closeConnection()
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- conn.Dispose();
- comm.Dispose();
- }
- }//關閉數據庫
- public static void excuteSql(string sqlstr)
- {
- try
- {
- openConnection();
- comm.CommandType = CommandType.Text;
- comm.CommandText = sqlstr;
- comm.ExecuteNonQuery();
- }
- catch (Exception e)
- {
- throw new Exception(e.Message);
- }
- finally
- { closeConnection(); }
- }//執行sql語句
- 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;
- }//返回指定sql語句的OleDbDataReader對象,使用時請注意關閉這個對象。
- 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();
- }
- }
- }//返回指定sql語句的OleDbDataReader對象,使用時請注意關閉
- 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;
- }//返回指定sql語句的dataset
- 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();
- }
- }//返回指定sql語句的dataset
- public static DataTable dataTable(string sqlstr)
- {
- DataTable dt = new DataTable();
- 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();
- }
- return dt;
- }//返回指定sql語句的datatable
- 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();
- }
- }//返回指定sql語句的datatable
- 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;
- }
- //返回指定sql語句的dataview
- }
- }
通用數據訪問層--Access版
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.