C#操作Access实例是怎么实现的呢?让我们来看看具体的代码:

成都创新互联公司专业为企业提供仪征网站建设、仪征做网站、仪征网站设计、仪征网站制作等企业网站建设、网页设计与制作、仪征企业网站模板建站服务,十余年仪征做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
- 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;
 - ///
 - /// DataAccess 的摘要说明 C#操作Access实例解析
 - ///
 - public class DataAccess
 - {
 - protected static OleDbConnection conn = new OleDbConnection();
 - protected static OleDbCommand comm = new OleDbCommand();
 - public DataAccess()
 - {
 - //init C#操作Access实例解析
 - }
 - private static void openConnection()
 - {
 - if (conn.State == ConnectionState.Closed)
 - {
 - conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;
 - Data Source="+ConfigurationManager.AppSettings["myconn"];
 - //web.config文件里设定。
 - comm.Connection = conn;
 - try
 - {
 - conn.Open();
 - }
 - catch (Exception e)
 - { throw new Exception(e.Message); }
 - }
 - }//打开数据库 C#操作Access实例解析
 - private static void closeConnection()
 - {
 - if (conn.State == ConnectionState.Open)
 - {
 - conn.Close();
 - conn.Dispose();
 - comm.Dispose();
 - }
 - }//关闭数据库 C#操作Access实例解析
 - 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语句 C#操作Access实例解析
 - 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();
 - } //C#操作Access实例解析
 - 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 C#操作Access实例解析
 - 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 C#操作Access实例解析
 - 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 C#操作Access实例解析
 - 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 C#操作Access实例解析
 - }
 
C#操作Access实例解析的基本内容就向你介绍到这里,希望对你了解和学习C#操作Access有所帮助。
【编辑推荐】
                网站题目:C#操作Access实例解析
                
                URL链接:http://www.csdahua.cn/qtweb/news9/522059.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网