ASP.NET 数据访问类

时间:2021-05-28

usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;

namespaceSysClassLibrary
{
///<summary>
///DataAccess的摘要说明。
///<description>数据处理基类,调用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,refDataSetds);</description>
///</summary>
publicclassDataAccess
{
#region属性
protectedstaticSqlConnectionconn=newSqlConnection();
protectedstaticSqlCommandcomm=newSqlCommand();
#endregion
publicDataAccess()
{
//init();
}
#region内部函数静态方法中不会执行DataAccess()构造函数

///<summary>
///打开数据库连接
///</summary>
privatestaticvoidopenConnection()
{
if(conn.State==ConnectionState.Closed)
{
//SysConfig.ConnectionString为系统配置类中连接字符串,如:"server=localhost;database=databasename;uid=sa;pwd=;"

conn.ConnectionString=SysConfig.ConnectionString;
comm.Connection=conn;
try
{
conn.Open();
}
catch(Exceptione)
{
thrownewException(e.Message);
}
}
}
///<summary>
///关闭当前数据库连接
///</summary>
privatestaticvoidcloseConnection()
{
if(conn.State==ConnectionState.Open)
conn.Close();
conn.Dispose();
comm.Dispose();
}
#endregion
///<summary>
///执行Sql查询语句
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
publicstaticvoidExecuteSql(stringsqlstr)
{
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText=sqlstr;
comm.ExecuteNonQuery();
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
closeConnection();
}
}

///<summary>
///执行存储过程
///</summary>
///<paramname="procName">存储过程名</param>
///<paramname="coll">SqlParameters集合</param>
publicstaticvoidExecutePorcedure(stringprocName,SqlParameter[]coll)
{
try
{
openConnection();
for(inti=0;i<coll.Length;i++)
{
comm.Parameters.Add(coll);
}
comm.CommandType=CommandType.StoredProcedure;
comm.CommandText=procName;
comm.ExecuteNonQuery();
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
comm.Parameters.Clear();
closeConnection();
}
}

///<summary>
///执行存储过程并返回数据集
///</summary>
///<paramname="procName">存储过程名称</param>
///<paramname="coll">SqlParameter集合</param>
///<paramname="ds">DataSet</param>
publicstaticvoidExecutePorcedure(stringprocName,SqlParameter[]coll,refDataSetds)
{
try
{
SqlDataAdapterda=newSqlDataAdapter();
openConnection();
for(inti=0;i<coll.Length;i++)
{
comm.Parameters.Add(coll);
}
comm.CommandType=CommandType.StoredProcedure;
comm.CommandText=procName;

da.SelectCommand=comm;
da.Fill(ds);
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
comm.Parameters.Clear();
closeConnection();
}
}

///<summary>
///执行Sql查询语句并返回第一行的第一条记录,返回值为object使用时需要拆箱操作->Unbox
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
///<returns>object返回值</returns>
publicstaticobjectExecuteScalar(stringsqlstr)
{
objectobj=newobject();
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText=sqlstr;
obj=comm.ExecuteScalar();
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
closeConnection();
}
returnobj;
}

///<summary>
///执行Sql查询语句,同时进行事务处理
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
publicstaticvoidExecuteSqlWithTransaction(stringsqlstr)
{
SqlTransactiontrans;
trans=conn.BeginTransaction();
comm.Transaction=trans;
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText=sqlstr;
comm.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
}
finally
{
closeConnection();
}
}

///<summary>
///返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
///方法关闭数据库连接
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
///<returns>SqlDataReader对象</returns>
publicstaticSqlDataReaderdataReader(stringsqlstr)
{
SqlDataReaderdr=null;
try
{
openConnection();
comm.CommandText=sqlstr;
comm.CommandType=CommandType.Text;
dr=comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
dr.Close();
closeConnection();
}
catch
{
}
}
returndr;
}
///<summary>
///返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接
///方法关闭数据库连接
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
///<paramname="dr">传入的refDataReader对象</param>
publicstaticvoiddataReader(stringsqlstr,refSqlDataReaderdr)
{
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();
}
}
}

///<summary>
///返回指定Sql语句的DataSet
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
///<returns>DataSet</returns>
publicstaticDataSetdataSet(stringsqlstr)
{
DataSetds=newDataSet();
SqlDataAdapterda=newSqlDataAdapter();
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText=sqlstr;
da.SelectCommand=comm;
da.Fill(ds);
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
closeConnection();
}
returnds;
}

///<summary>
///返回指定Sql语句的DataSet
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
///<paramname="ds">传入的引用DataSet对象</param>
publicstaticvoiddataSet(stringsqlstr,refDataSetds)
{
SqlDataAdapterda=newSqlDataAdapter();
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText=sqlstr;
da.SelectCommand=comm;
da.Fill(ds);
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
closeConnection();
}
}
///<summary>
///返回指定Sql语句的DataTable
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
///<returns>DataTable</returns>
publicstaticDataTabledataTable(stringsqlstr)
{
SqlDataAdapterda=newSqlDataAdapter();
DataTabledatatable=newDataTable();
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText=sqlstr;
da.SelectCommand=comm;
da.Fill(datatable);
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
closeConnection();
}
returndatatable;
}

///<summary>
///执行指定Sql语句,同时给传入DataTable进行赋值
///</summary>
///<paramname="sqlstr">传入的Sql语句</param>
///<paramname="dt">refDataTabledt</param>
publicstaticvoiddataTable(stringsqlstr,refDataTabledt)
{
SqlDataAdapterda=newSqlDataAdapter();
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText=sqlstr;
da.SelectCommand=comm;
da.Fill(dt);
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
closeConnection();
}
}
///<summary>
///执行带参数存储过程并返回数据集合
///</summary>
///<paramname="procName">存储过程名称</param>
///<paramname="parameters">SqlParameterCollection输入参数</param>
///<returns></returns>
publicstaticDataTabledataTable(stringprocName,SqlParameterCollectionparameters)
{
SqlDataAdapterda=newSqlDataAdapter();
DataTabledatatable=newDataTable();

try
{
openConnection();
comm.Parameters.Clear();
comm.CommandType=CommandType.StoredProcedure;
comm.CommandText=procName;
foreach(SqlParameterparainparameters)
{
SqlParameterp=(SqlParameter)para;
comm.Parameters.Add(p);
}
da.SelectCommand=comm;
da.Fill(datatable);
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
closeConnection();
}
returndatatable;
}

publicstaticDataViewdataView(stringsqlstr)
{
SqlDataAdapterda=newSqlDataAdapter();
DataViewdv=newDataView();
DataSetds=newDataSet();
try
{
openConnection();
comm.CommandType=CommandType.Text;
comm.CommandText=sqlstr;
da.SelectCommand=comm;
da.Fill(ds);
dv=ds.Tables[0].DefaultView;
}
catch(Exceptione)
{
thrownewException(e.Message);
}
finally
{
closeConnection();
}
returndv;
}
}

}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章