/// <summary> /// sql参数封装类。 /// 可以用于设置sql语句动态参数。 /// </summary> class SqlParamsTool {
private List<SqlParameter> paramList = new List<SqlParameter>();//sql参数集合,用于保存设置的参数 private static SqlParamsTool tool; private SqlParamsTool() { } /// <summary> /// 获取SqlParameterTool的实例 /// </summary> /// <returns></returns> public static SqlParamsTool GetSqlParamsToolInstance() { return tool = new SqlParamsTool(); } /// <summary> /// 返回设置的所有参数数组形式。 /// 如果没有设置参数,则返回null值。 /// </summary> /// <returns></returns> public SqlParameter[] GetSqlParameters() { if (paramList.Count == 0) { return null; } return paramList.ToArray(); } /// <summary> /// 清空设置的所有sql参数 /// </summary> public void ClearParamList() { paramList.Clear(); } //>>>设置参数:
public SqlParamsTool SetStringParam(string name ,string value ) { this.SetObjectParam(name,value,SqlDbType.VarChar); return tool; } public SqlParamsTool SetIntParam(string name, int value) { this.SetObjectParam(name, value, SqlDbType.Int); return tool; } public SqlParamsTool SetFloatParam(string name, float value) { this.SetObjectParam(name, value, SqlDbType.Float); return tool; } public SqlParamsTool SetDecimalParam(string name, decimal value) { this.SetObjectParam(name, value, SqlDbType.Decimal); return tool; } public SqlParamsTool SetDateTimeParam(string name, DateTime value) { this.SetObjectParam(name, value, SqlDbType.DateTime); return tool; } public SqlParamsTool SetObjectParam(string paramName, object paramValue, SqlDbType paramType) { SqlParameter sp = new SqlParameter(paramName, paramValue); sp.SqlDbType = paramType; paramList.Add(sp); return tool; } }