关于GridView控件的使用
在DAL中插入如下代码
public static List<UserInfo> Select(string name=" ",int role=-1)
{
var sql=$"select * from UserInfo where1=1";
SqlParameter[ ] sqlParameters=new SqlParameter[2 ];
if(!string.IsNullOrWhiteSpace(name))
{
sql+="and Name like '@name' ";
sqlParameters.Add(new SqlParameter("@name","%"+name+"%"));
}
if(role!=-1)
{
sql+="and role=@role";
sqlParameters.Add(new SqlParameter("@role",role))
}
var reader=SqlHelper.ExecReader(sql,sqlParameters.ToArray());
List<UserInfo> list=new List<UserInfo>();
while(reader.Read())
{
UserInfo user=new UserInfo ()
{
Id=Convert.ToInt32(reader["Id"]),
Name=Convert.ToString(reader["Name"]),
Role=Convert.ToInt32(reader["Role"])
};
list.Add(user);
}
return list;
}
在BLL中调用DAL中的方法
public static List<UserInfo> Select(string name=" ",int role=-1)
{
return UserInfoDAL.Select(name,role);
}
绑定数据
private void Bind(string name=" ",int role=-1)
{
GridView.DataSource=UserInfoBLL.Select(name,role);
GridView.DataBind();
}