C#中怎么操作Access
这期内容当中小编将会给大家带来有关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
新闻来源:http://pwwzsj.com/article/psdseh.html