微软提供的Data Access Application Block中的SQLHelper类中封装了最常用的数据操作,各个使用者调用他而写的代码也有很大区别。
对于一个返回DataSet的方法我原来是这样写的:
public DataSet GetDepartmentMemberList(int departmentID)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sql = "OA_Department_GetDepartment_Members";
string conn = ConfigurationSettings.AppSettings["strConnection"];
SqlParameter[] p =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlHelper.MakeInParam("@departmentID",SqlDbType.Int,4,departmentID)
};
DataSet ds = SqlHelper.ExecuteDataset(conn,CommandType.StoredProcedure,sql,p);
return ds;
}
catch(System.Data.SqlClient.SqlException er)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(er.Message);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
现在我是这样来写的:
//连接字符串
private string _connectionString = ConfigurationSettings.AppSettings["strConnection"];
public string ConnectionString
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this._connectionString;}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this._connectionString = value;}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public DataSet GetNewsToIndexPage(int NewsTypeID)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
return GetDataSet("yzb_GetNewsToIndexPage",GetNewsToIndexPage_Parameters(NewsTypeID));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//设置存储过程参数
private SqlParameter[] GetNewsToIndexPage_Parameters(int NewsTypeID)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlParameter[] p =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlHelper.MakeInParam("@NewsTypeID",SqlDbType.Int,4,NewsTypeID)
};
return p;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//这里才真正调用SqlHelper
private DataSet GetDataSet(string sql, params SqlParameter[] p)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
return SqlHelper.ExecuteDataset(ConnectionString,CommandType.StoredProcedure,sql,p);
}
代码更加灵活,更加安全了:P
反馈
# re: 更好的使用SQLHelper类
2004-8-10 16:28 by
看不出有啥区别
# re: 更好的使用SQLHelper类
2004-8-10 23:01 by
朋友还需努力啊:)
# re: 更好的使用SQLHelper类
2005-6-15 4:30 by
呵呵,我习惯这样写:
System.Data.DataTable dt=SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING_NON_DTC,CommandType.Text,sql).Tables[0];
直接返回DataTable, 返回表集合基本不用,只返回一个table。
string conn = ConfigurationSettings.AppSettings["strConnection"];
我写在SqlHelper.CONN_STRING_NON_DTC里面
MakeInParam代码烦琐,如果查询输入条件不可能有组合SQL,
我直接exec pronamr paraargs
如果查询输入条件存在安全问题,或者带返回参数
再用SqlParameter[] queryParam=new SqlParameter[] {
new SqlParameter("@UserID",SqlDbType.Int)
};
queryParam[0].Value=this.UserID;
# re: 更好的使用SQLHelper类
2005-8-15 17:51 by
把简单的东西复杂化了
参数集合没必要再独立出去
想想几十个方法几十种参数
搞得都会乱掉的
不好阅读也不好维护
# re: 更好的使用SQLHelper类
2005-8-30 15:14 by
这样写
public string ConnectionString
{
get {return this._connectionString;}
set {this._connectionString = value;}
}
是不合适的,最多是
public string ConnectionString
{
get {return this._connectionString;}
}
把它设置为一个只读属性,以网址后面程序在代码里设置它的值,明白不? :)