为ASP.NET封装的SQL数据库访问类

usingSystem;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Collections;

namespaceMyCorporation.DepartMent.DataBase
{
///<summary>
///通用数据库类
///</summary>
publicclassDataBase
{

privatestringConnStr=null;

publicDataBase()
{
ConnStr=ConfigurationSettings.AppSettings["ConnStr"];
}
publicDataBase(stringStr)
{
try
{
this.ConnStr=Str;

}
catch(Exceptionex)
{
throwex;
}
}

///<summary>
///返回connection对象
///</summary>
///<returns></returns>
publicSqlConnectionReturnConn()
{
SqlConnectionConn=newSqlConnection(ConnStr);
Conn.Open();
returnConn;
}
publicvoidDispose(SqlConnectionConn)
{
if(Conn!=null)
{
Conn.Close();
Conn.Dispose();
}
GC.Collect();
}
///<summary>
///运行SQL语句
///</summary>
///<paramname="SQL"></param>
publicvoidRunProc(stringSQL)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlCommandCmd;
Cmd=CreateCmd(SQL,Conn);
try
{
Cmd.ExecuteNonQuery();
}
catch
{
thrownewException(SQL);
}
Dispose(Conn);
return;
}

///<summary>
///运行SQL语句返回DataReader
///</summary>
///<paramname="SQL"></param>
///<returns>SqlDataReader对象.</returns>
publicSqlDataReaderRunProcGetReader(stringSQL)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlCommandCmd;
Cmd=CreateCmd(SQL,Conn);
SqlDataReaderDr;
try
{
Dr=Cmd.ExecuteReader(CommandBehavior.Default);
}
catch
{
thrownewException(SQL);
}
//Dispose(Conn);
returnDr;
}

///<summary>
///生成Command对象
///</summary>
///<paramname="SQL"></param>
///<paramname="Conn"></param>
///<returns></returns>
publicSqlCommandCreateCmd(stringSQL,SqlConnectionConn)
{
SqlCommandCmd;
Cmd=newSqlCommand(SQL,Conn);
returnCmd;
}

///<summary>
///生成Command对象
///</summary>
///<paramname="SQL"></param>
///<returns></returns>
publicSqlCommandCreateCmd(stringSQL)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlCommandCmd;
Cmd=newSqlCommand(SQL,Conn);
returnCmd;
}

///<summary>
///返回adapter对象
///</summary>
///<paramname="SQL"></param>
///<paramname="Conn"></param>
///<returns></returns>
publicSqlDataAdapterCreateDa(stringSQL)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlDataAdapterDa;
Da=newSqlDataAdapter(SQL,Conn);
returnDa;
}

///<summary>
///运行SQL语句,返回DataSet对象
///</summary>
///<paramname="procName">SQL语句</param>
///<paramname="prams">DataSet对象</param>
publicDataSetRunProc(stringSQL,DataSetDs)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlDataAdapterDa;
//Da=CreateDa(SQL,Conn);
Da=newSqlDataAdapter(SQL,Conn);
try
{
Da.Fill(Ds);
}
catch(ExceptionErr)
{
throwErr;
}
Dispose(Conn);
returnDs;
}

///<summary>
///运行SQL语句,返回DataSet对象
///</summary>
///<paramname="procName">SQL语句</param>
///<paramname="prams">DataSet对象</param>
///<paramname="dataReader">表名</param>
publicDataSetRunProc(stringSQL,DataSetDs,stringtablename)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlDataAdapterDa;
Da=CreateDa(SQL);
try
{
Da.Fill(Ds,tablename);
}
catch(ExceptionEx)
{
throwEx;
}
Dispose(Conn);
returnDs;
}

///<summary>
///运行SQL语句,返回DataSet对象
///</summary>
///<paramname="procName">SQL语句</param>
///<paramname="prams">DataSet对象</param>
///<paramname="dataReader">表名</param>
publicDataSetRunProc(stringSQL,DataSetDs,intStartIndex,intPageSize,stringtablename)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlDataAdapterDa;
Da=CreateDa(SQL);
try
{
Da.Fill(Ds,StartIndex,PageSize,tablename);
}
catch(ExceptionEx)
{
throwEx;
}
Dispose(Conn);
returnDs;
}

///<summary>
///检验是否存在数据
///</summary>
///<returns></returns>
publicboolExistDate(stringSQL)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlDataReaderDr;
Dr=CreateCmd(SQL,Conn).ExecuteReader();
if(Dr.Read())
{
Dispose(Conn);
returntrue;
}
else
{
Dispose(Conn);
returnfalse;
}
}

///<summary>
///返回SQL语句执行结果的第一行第一列
///</summary>
///<returns>字符串</returns>
publicstringReturnValue(stringSQL)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
stringresult;
SqlDataReaderDr;
try
{
Dr=CreateCmd(SQL,Conn).ExecuteReader();
if(Dr.Read())
{
result=Dr[0].ToString();
Dr.Close();
}
else
{
result="";
Dr.Close();
}
}
catch
{
thrownewException(SQL);
}
Dispose(Conn);
returnresult;
}

///<summary>
///返回SQL语句第一列,第ColumnI列,
///</summary>
///<returns>字符串</returns>
publicstringReturnValue(stringSQL,intColumnI)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
stringresult;
SqlDataReaderDr;
try
{
Dr=CreateCmd(SQL,Conn).ExecuteReader();
}
catch
{
thrownewException(SQL);
}
if(Dr.Read())
{
result=Dr[ColumnI].ToString();
}
else
{
result="";
}
Dr.Close();
Dispose(Conn);
returnresult;
}

///<summary>
///生成一个存储过程使用的sqlcommand.
///</summary>
///<paramname="procName">存储过程名.</param>
///<paramname="prams">存储过程入参数组.</param>
///<returns>sqlcommand对象.</returns>
publicSqlCommandCreateCmd(stringprocName,SqlParameter[]prams)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlCommandCmd=newSqlCommand(procName,Conn);
Cmd.CommandType=CommandType.StoredProcedure;
if(prams!=null)
{
foreach(SqlParameterparameterinprams)
{
if(parameter!=null)
{
Cmd.Parameters.Add(parameter);
}
}
}
returnCmd;
}

///<summary>
///为存储过程生成一个SqlCommand对象
///</summary>
///<paramname="procName">存储过程名</param>
///<paramname="prams">存储过程参数</param>
///<returns>SqlCommand对象</returns>
privateSqlCommandCreateCmd(stringprocName,SqlParameter[]prams,SqlDataReaderDr)
{
SqlConnectionConn;
Conn=newSqlConnection(ConnStr);
Conn.Open();
SqlCommandCmd=newSqlCommand(procName,Conn);
Cmd.CommandType=CommandType.StoredProcedure;
if(prams!=null)
{
foreach(SqlParameterparameterinprams)
Cmd.Parameters.Add(parameter);
}
Cmd.Parameters.Add(
newSqlParameter("ReturnValue",SqlDbType.Int,4,
ParameterDirection.ReturnValue,false,0,0,
string.Empty,DataRowVersion.Default,null));

returnCmd;
}

///<summary>
///运行存储过程,返回.
///</summary>
///<paramname="procName">存储过程名</param>
///<paramname="prams">存储过程参数</param>
///<paramname="dataReader">SqlDataReader对象</param>
publicvoidRunProc(stringprocName,SqlParameter[]prams,SqlDataReaderDr)
{

SqlCommandCmd=CreateCmd(procName,prams,Dr);
Dr=Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return;
}

///<summary>
///运行存储过程,返回.
///</summary>
///<paramname="procName">存储过程名</param>
///<paramname="prams">存储过程参数</param>
publicstringRunProc(stringprocName,SqlParameter[]prams)
{
SqlDataReaderDr;
SqlCommandCmd=CreateCmd(procName,prams);
Dr=Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if(Dr.Read())
{
returnDr.GetValue(0).ToString();
}
else
{
return"";
}
}

///<summary>
///运行存储过程,返回dataset.
///</summary>
///<paramname="procName">存储过程名.</param>
///<paramname="prams">存储过程入参数组.</param>
///<returns>dataset对象.</returns>
publicDataSetRunProc(stringprocName,SqlParameter[]prams,DataSetDs)
{
SqlCommandCmd=CreateCmd(procName,prams);
SqlDataAdapterDa=newSqlDataAdapter(Cmd);
try
{
Da.Fill(Ds);
}
catch(ExceptionEx)
{
throwEx;
}
returnDs;
}

}
}

上一篇:C# 非托管DLL调用


下一篇:.net 中的托管与非托管