C#操作Access实例解析
2013-02-25 21:38
393 查看
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实例解析 }
C#操作Access实例解析的基本内容就向你介绍到这里,希望对你了解和学习C#操作Access有所帮助。
相关文章推荐
- C#的WebBrowser操作frame实例解析
- C#操作Access通用类实例
- C#打印原理解析及实例操作
- C#操作Access通用类实例
- C#操作Access通用类实例
- C#的WebBrowser操作frame实例解析
- C# 操作 access 数据库的实例代码
- C#操作XML实例解析(创建、增、删、改)
- C#指针操作Marshal实例
- VC实现ODBC数据库操作实例解析
- 路由及路由器工作原理深入解析4:路由器操作实例
- C# 常见Url操作实例(二)
- C#连接操作mysql实例
- C# 操作 ACCESS
- Java BufferedInputStream与BufferedOutputStream 入门版实例解析【文件操作】
- c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(三)
- C#操作mysql数据库的代码实例
- JS操作XML实例总结(加载与解析XML文件、字符串)
- Access Operation Using ADOX and OleDb(C#操作Access数据库)
- C#发送邮件的实现实例解析