查看:public sealed class SqlParameterCollection : DbParameterCollection类
//1。定义封装类
class Param
{
private List<SqlParameter> paramList=new List<SqlParameter>();
public SqlParameter[] ParamList{
get{
return paramList.ToArray();
}
}
/// <summary>
/// 增添类型为VarChar预处理值
/// </summary>
/// <param name="ParamName"></param>
/// <param name="value"></param>
public void AddChar(string ParamName,string value)
{
SqlParameter p=new SqlParameter(ParamName,SqlDbType.VarChar);
p.Value=value;
paramList.Add(p);
}
public void CleanParam()
{
paramList.Clear();
}
/// <summary>
/// 增添类型为Int预处理值
/// </summary>
/// <param name="ParamName"></param>
/// <param name="value"></param>
public void AddInt(string ParamName, int value)
{
SqlParameter p = new SqlParameter(ParamName, SqlDbType.Int);
p.Value = value;
paramList.Add(p);
}
/// <summary>
/// 增添类型为float预处理值
/// </summary>
/// <param name="ParamName"></param>
/// <param name="value"></param>
public void AddFloat(string ParamName, float value)
{
SqlParameter p = new SqlParameter(ParamName, SqlDbType.Float);
p.Value = value;
paramList.Add(p);
}
}
//2。使用举例
查看:public sealed class SqlParameterCollection : DbParameterCollection类
/// <summary>
/// 获取数据,进行数据的绑定
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="p">预处理的值</param>
/// <returns></returns>
public DataTable DataList(string sql, SqlParameter[] p)
{
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.Parameters.AddRange(p);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0];
}
//3。 注意
使用集合参数paramList,在使用之前需要将集合清空 ,防止在sql 语句中增添无效参数
paramList.Clear();