C#操作Access实例解析

开发 后端
C#操作Access实例解析向你介绍了C#操作Access的全过程,希望通过实例演示,能使你对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;   
 
/// <summary>  
/// DataAccess 的摘要说明 C#操作Access实例解析 
/// </summary>  
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实例解析 
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.

C#操作Access实例解析的基本内容就向你介绍到这里,希望对你了解和学习C#操作Access有所帮助。

【编辑推荐】

  1. C#操作注册表常用方法详解
  2. C#操作Access之创建mdb库浅析
  3. C#操作Access之创建表浅析
  4. C#操作Access之读取mdb浅析
  5. C#操作Access之按列读取mdb浅析
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-19 16:30:55

C#操作Access数

2009-09-01 13:59:01

C#操作Excel

2009-08-26 14:03:26

C#打印原理

2009-09-09 14:40:15

C# XML解析

2024-04-18 09:56:16

2009-09-07 06:31:32

C#窗体移动

2009-08-26 12:14:44

C#打印设置

2009-08-31 18:17:32

C#接口编程

2009-08-18 10:47:40

C#枚举类型

2009-09-09 13:57:28

C# XML解析

2009-08-20 11:01:51

C#操作内存

2009-08-18 13:49:21

C# 操作Excel

2009-08-19 15:47:09

C#操作Access

2009-08-19 15:55:42

C#操作Access

2009-09-14 14:25:53

C# Lambda EC# Lambda

2009-09-01 18:32:32

C#动态数组

2009-09-03 15:43:21

C#时间计算

2009-09-01 13:51:51

C#创建Word文档

2009-09-03 09:16:35

C#递归函数

2009-08-31 17:30:10

C#接口的作用
点赞
收藏

51CTO技术栈公众号