zoukankan      html  css  js  c++  java
  • C#的Access_Helper_类似sqlhelper

    数据库引擎需要Office2007以上版本

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;


    /// <summary>
    /// AccessHelper 的摘要说明
    /// </summary>
    public class AccessHelper
    {
    protected static OleDbConnection conn = new OleDbConnection();
    protected static OleDbCommand comm = new OleDbCommand();

    public AccessHelper()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }

    /// <summary>
    /// 打开数据库
    /// </summary>
    private static void openConnection()
    {
    if (conn.State == ConnectionState.Closed)
    {
    conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory+ConfigurationManager.AppSettings["myconn"].ToString() + ";Jet OLEDB:Database PassWord=sa";
    comm.Connection = conn;
    try
    {
    conn.Open();
    }
    catch (Exception e)
    { throw new Exception(e.Message); }

    }

    }
    /// <summary>
    /// 关闭数据库
    /// </summary>
    private static void closeConnection()
    {
    if (conn.State == ConnectionState.Open)
    {
    conn.Close();
    conn.Dispose();
    comm.Dispose();
    }
    }
    /// <summary>
    /// 执行sql语句
    /// </summary>
    /// <param name="sqlstr"></param>
    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(); }
    }
    /// <summary>
    /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    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;
    }
    /// <summary>
    /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="dr"></param>
    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();
    }
    }
    }
    /// <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;
    }
    /// <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();
    }
    }
    /// <summary>
    /// 返回指定sql语句的datatable
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    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;
    }
    /// <summary>
    /// 返回指定sql语句的datatable
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="dt"></param>
    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();
    }
    }
    /// <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;
    }
    }

    web.config配置

    <configuration>
    <appSettings>
    <add key="myconn" value="数据库文件相对路径" />
    </appSettings>
    </configuration>



  • 相关阅读:
    设计模式——观察者模式
    安卓xml动画
    部署在weblogic上的springboot项目上传文件(servlet方式)
    Spring Boot 部署到weblogic 12c
    SpingBoot+Druid监控页面打不开(404)
    DAY49-前端入门-浮动布局案例、z-index、flex布局、响应式布局、过渡与动画
    DAY48-前端入门-文档流、浮动布局、清浮动、流式布局、定位布局
    DAY46-前端入门-组合选择器、标签a_img_list、盒模型、伪类、盒模型布局
    DAY45-前端入门-css的三种引用方式以及优先级、样式与长度颜色、常用样式、css选择器
    DAY44-前端入门-前端三剑客、第一个页面、常用标签、标签分类
  • 原文地址:https://www.cnblogs.com/shenyixin/p/2365071.html
Copyright © 2011-2022 走看看