Access 通用数据访问类(asp.net 2.0 c#)

王朝c#·作者佚名  2006-11-24
宽屏版  字体: |||超大  

仿照以前收集的一个经典sql server数据访问类,稍做修改。

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 的摘要说明

/// </summary>

public class DataAccess

{

protected static OleDbConnection conn = new OleDbConnection();

protected static OleDbCommand comm = new OleDbCommand();

public DataAccess()

{

//init

}

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); }

}

}//打开数据库

private static void closeConnection()

{

if (conn.State == ConnectionState.Open)

{

conn.Close();

conn.Dispose();

comm.Dispose();

}

}//关闭数据库

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语句

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();

}

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)

{

&

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有