SqlParameter[]写法
- //封装SqlParameter 一次性一行添加sql参数 包括类型,尺寸,进出...
- public static SqlParameter AddParameter(string parameterName, object parameterValue, SqlDbType dbType, int parameterSize, ParameterDirection Direction)
- {
- SqlParameter par = new SqlParameter();
- par.ParameterName = parameterName;
- par.Value = parameterValue;
- par.Direction = Direction;
- par.SqlDbType = dbType;
- if (parameterSize > 0) { par.Size = parameterSize; }
- return par;
- }
- public static SqlParameter AddInParameter(string parameterName, object parameterValue, SqlDbType dbType, int parameterSize)
- {
- return AddParameter(parameterName, parameterValue, dbType, parameterSize, ParameterDirection.Input);
- }
- public static SqlParameter AddInParameter(string parameterName, object parameterValue, SqlDbType dbType)
- {
- return AddParameter(parameterName, parameterValue, dbType, 0, ParameterDirection.Input);
- }
- public static SqlParameter AddOutParameter(string parameterName, SqlDbType dbType)
- {
- return AddParameter(parameterName, DBNull.Value, dbType, 0, ParameterDirection.Output);
- }
//调用:
public DataSet GetPageList2005(string SelectList,string TableSource,int PageSize,int PageIndex,stringstrWhere,string OrderExpression,refint TotalRecord)
{
SqlParameter[] parameters = {
DbHelperSQL.AddInParameter("@ReFieldsStr",SelectList,SqlDbType.VarChar,200),
DbHelperSQL.AddInParameter("@TableName", TableSource, SqlDbType.VarChar,50),
DbHelperSQL.AddInParameter("@WhereString",strWhere,SqlDbType.VarChar,500),
DbHelperSQL.AddInParameter("@OrderString", OrderExpression,SqlDbType.VarChar,200),
DbHelperSQL.AddInParameter("@PageSize",PageSize,SqlDbType.Int),
DbHelperSQL.AddInParameter("@PageIndex",PageIndex,SqlDbType.Int),
DbHelperSQL.AddOutParameter("@TotalRecord",SqlDbType.Int)
};
DataSet tmpds = DbHelperSQL.RunProcedure("GetPageList2005", parameters, "ds_"+ TableSource);
TotalRecord =int.Parse(parameters[6].Value.ToString());
return tmpds;
}
另一种:
SqlParameter[] params = new SqlParameter[] {
new SqlParameter("@a", a),
string.IsNullOrEmpty(b)? new SqlParameter("@b", DBNull.Value) : new SqlParameter("@b", b)
};
另一种:
List<SqlParameter> list= new List<SqlParameter>();
list.Add(new SqlParameter("@Param1", a));
list.Add(new SqlParameter("@Param2", b));
list.Add(new SqlParameter("@Param3", string.IsNullOrEmpty(c)?(object)DBNull.Value:c ));
SqlParameter[] Params = list.ToArray();