如果不使用ASP.NET数据访问层,那么你的代码里会出现很多SqlConnection、SqlCommand、SqlDataReader、Open、 Close……这些类和方法,而且代码量很大,让你不胜其烦,而且代码写起来,其实都是体力活,没有技术含量。
每个项目都要用到ASP.NET数据访问层,我做的也不例外,但是我把数据访问层做成独立项目,没有什么太大的目的,数据访问层,仅仅做数据访问用,不包含任何逻辑。
以下是自己改进的ASP.NET数据访问层,希望能得到更多人更好的改进建议,谢谢啦。
- usingSystem;
- usingSystem.Data;
- usingSystem.Data.SqlClient;
- usingSystem.Configuration;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.Text;
- ///<summary>
- ///数据访问层,数据库操作类
- ///summary>
- publicclassDatabase
- {
- publicstaticstringConnectionStrings=ConfigurationManager.
ConnectionStrings["ConnectionStrings"].ConnectionString;- ///<summary>
- ///执行带SqlParameter的sql语句,成功返回true,失败返回false
- ///summary>
- ///<paramnameparamname="sql">sql语句param>
- ///<paramnameparamname="Param">SqlParameter[]参数列表param>
- ///<returns>returns>
- publicstaticintExecuteNonQuery(stringsql,paramsSqlParameter[]Param)
- //执行带SqlParameter的sql语句,返回影响的行数
- {
- //HttpContext.Current.Response.Write(sql+"<br>");
- using(SqlConnectionconn=newSqlConnection(ConnectionStrings))
- {
- if(conn.State!=ConnectionState.Open)
- conn.Open();
- SqlTransactionTran=conn.BeginTransaction();//开始事务
- SqlCommandcmd=newSqlCommand(sql,conn,Tran);
- if(Param!=null)
- cmd.Parameters.AddRange(Param);
- try
- {
- intResult=cmd.ExecuteNonQuery();
- Tran.Commit();
- cmd.Parameters.Clear();
- returnResult;
- }
- catch(Exceptionerr)
- {
- HttpContext.Current.Response.Write(err.Message+"<br>"+err.StackTrace);
- Tran.Rollback();//回滚事务
- return0;
- }
- }
- }
- ///<summary>
- ///执行带SqlParameter的sql,返回DataReader
- ///summary>
- ///<paramnameparamname="sql">sql语句param>
- ///<paramnameparamname="Param">SqlParameter[]参数列表param>
- ///<returns>返回SqlDataReaderreturns>
- publicstaticSqlDataReaderExecuteReader(stringsql,paramsSqlParameter[]Param)
- //执行带SqlParameter的sql,返回DataReader
- {
- SqlConnectionconn=newSqlConnection(ConnectionStrings);
- SqlCommandcmd=newSqlCommand(sql,conn);
- if(Param!=null)
- cmd.Parameters.AddRange(Param);
- if(conn.State!=ConnectionState.Open)
- conn.Open();
- try
- {
- SqlDataReaderdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
- cmd.Parameters.Clear();
- returndr;
- }
- catch(Exceptionerr)
- {
- conn.Close();
- HttpContext.Current.Response.Write(err.Message+"<br>"+err.StackTrace);
- returnnull;
- }
- }
- ///<summary>
- ///执行带SqlParameter的sql,返回DataTable
- ///summary>
- ///<paramnameparamname="sql">SQL语句param>
- ///<paramnameparamname="Param">SqlParameter[]参数列表param>
- publicstaticDataTableGetDataTable(stringsql,paramsSqlParameter[]Param)
- //执行带SqlParameter的sql,返回DataTable
- {
- SqlConnectionconn=newSqlConnection(ConnectionStrings);
- DataTabledt=newDataTable();
- SqlDataAdapterda=newSqlDataAdapter(sql,conn);
- if(Param!=null)
- da.SelectCommand.Parameters.AddRange(Param);
- SqlCommandBuildercb=newSqlCommandBuilder(da);
- da.Fill(dt);
- conn.Close();
- //HttpContext.Current.Response.Write(sql+"<br>");
- returndt;
- }
- ///<summary>
- ///执行带SqlParameter的sql,返回查询结果***行***列的数据
- ///summary>
- ///<paramnameparamname="sql">SQL语句param>
- ///<paramnameparamname="Param">SqlParameter[]参数列表param>
- ///<returns>returns>
- publicstaticObjectExecuteScalar(stringsql,paramsSqlParameter[]Param)
- //执行带SqlParameter的sql,返回查询结果***行***列的数据
- {
- //HttpContext.Current.Response.Write(sql+"<br>");
- using(SqlConnectionconn=newSqlConnection(ConnectionStrings))
- {
- if(conn.State!=ConnectionState.Open)
- conn.Open();
- SqlTransactionTran=conn.BeginTransaction();
- SqlCommandcmd=newSqlCommand(sql,conn,Tran);
- if(Param!=null)
- cmd.Parameters.AddRange(Param);
- try
- {
- objectResult=cmd.ExecuteScalar();
- cmd.Parameters.Clear();
- Tran.Commit();
- returnResult;
- }
- catch(Exceptionerr)
- {
- HttpContext.Current.Response.Write(err.Message+"<br>"+err.StackTrace);
- Tran.Rollback();
- returnnull;
- }
- }
- }
- ///<summary>
- ///生成SqlParameter参数
- ///summary>
- ///<paramnameparamname="ParamName">参数名param>
- ///<paramnameparamname="paramType">参数类型param>
- ///<paramnameparamname="ParamValue">参数的值param>
- publicstaticSqlParameterGetParameter
(stringParamName,SqlDbTypeparamType,ObjectParamValue)- //生成SqlParameter参数
- {
- SqlParameterparam=newSqlParameter(ParamName,paramType);
- param.Value=ParamValue;
- returnparam;
- }
- }
【编辑推荐】