word = reader.GetString(reader.GetOrdinal("password")); } reader.Close(); return hc; } 现在想根据集合UserInfo内属性来添加SqlParameter参数
方法如下
DAL层方法 代码如下: public UserInfo GetALL(UserInfo a) { string strSql = "select id,name,code,password from [tb].[dbo].[User] where 1=1"; if (a.id>0) strSql += " and [id][email protected]"; if (!string.IsNullOrEmpty(a.name)) strSql += " and [name][email protected]"; if (!string.IsNullOrEmpty(a.code)) strSql += " and [code][email protected]"; if (!string.IsNullOrEmpty(a.password)) strSql += " and [password][email protected]"; List<SqlParameter> parametertemp = new List<SqlParameter>(); if (a.id > 0) parametertemp.Add(new SqlParameter("@id", a.id)); if (!string.IsNullOrEmpty(a.name)) parametertemp.Add(new SqlParameter("@name", a.name)); if (!string.IsNullOrEmpty(a.code)) parametertemp.Add(new SqlParameter("@code", a.code)); if (!string.IsNullOrEmpty(a.password)) parametertemp.Add(new SqlParameter("@password", a.password)); SqlParameter[] parameters = parametertemp.ToArray();//ToArray()方法将 List<T> 的元素复制到新数组中。
SqlDataReader reader = SqlHelper.ExecuteReader(strSql, parameters); UserInfo hc = new UserInfo(); while (reader.Read()) { hc.id = reader.GetInt32(reader.GetOrdinal("id"));