更好的使用SQLHelper类
微软提供的Data Access Application Block中的SQLHelper类中封装了最常用的数据操作,各个使用者调用他而写的代码也有很大区别。
对于一个返回DataSet的方法我原来是这样写的:
public DataSet GetDepartmentMemberList(int departmentID)
{
try
{
string sql = "OA_Department_GetDepartment_Members";
string conn = ConfigurationSettings.AppSettings["strConnection"];
SqlParameter[] p =
{
SqlHelper.MakeInParam("@departmentID",SqlDbType.Int,4,departmentID)
};
DataSet ds = SqlHelper.ExecuteDataset(conn,CommandType.StoredProcedure,sql,p);
return ds;
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}
}
{
try
{
string sql = "OA_Department_GetDepartment_Members";
string conn = ConfigurationSettings.AppSettings["strConnection"];
SqlParameter[] p =
{
SqlHelper.MakeInParam("@departmentID",SqlDbType.Int,4,departmentID)
};
DataSet ds = SqlHelper.ExecuteDataset(conn,CommandType.StoredProcedure,sql,p);
return ds;
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}
}
现在我是这样来写的:
//连接字符串
private string _connectionString = ConfigurationSettings.AppSettings["strConnection"];
public string ConnectionString
{
get {return this._connectionString;}
set {this._connectionString = value;}
}
public DataSet GetNewsToIndexPage(int NewsTypeID)
{
return GetDataSet("yzb_GetNewsToIndexPage",GetNewsToIndexPage_Parameters(NewsTypeID));
}
//设置存储过程参数
private SqlParameter[] GetNewsToIndexPage_Parameters(int NewsTypeID)
{
SqlParameter[] p =
{
SqlHelper.MakeInParam("@NewsTypeID",SqlDbType.Int,4,NewsTypeID)
};
return p;
}
//这里才真正调用SqlHelper
private DataSet GetDataSet(string sql, params SqlParameter[] p)
{
return SqlHelper.ExecuteDataset(ConnectionString,CommandType.StoredProcedure,sql,p);
}
private string _connectionString = ConfigurationSettings.AppSettings["strConnection"];
public string ConnectionString
{
get {return this._connectionString;}
set {this._connectionString = value;}
}
public DataSet GetNewsToIndexPage(int NewsTypeID)
{
return GetDataSet("yzb_GetNewsToIndexPage",GetNewsToIndexPage_Parameters(NewsTypeID));
}
//设置存储过程参数
private SqlParameter[] GetNewsToIndexPage_Parameters(int NewsTypeID)
{
SqlParameter[] p =
{
SqlHelper.MakeInParam("@NewsTypeID",SqlDbType.Int,4,NewsTypeID)
};
return p;
}
//这里才真正调用SqlHelper
private DataSet GetDataSet(string sql, params SqlParameter[] p)
{
return SqlHelper.ExecuteDataset(ConnectionString,CommandType.StoredProcedure,sql,p);
}
代码更加灵活,更加安全了:P
反馈
呵呵,我习惯这样写:
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;
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;