DAl:
#region 查询拼接
public DataView SelectAppend(string username, string sex, string permission)
{
string procedure = "exec dbo.zmt_procedure @username,@sex,@permission";
SqlParameter sqlparameter1 = new SqlParameter("@username", string.Format("%{0}%", username));
SqlParameter sqlparameter2 = new SqlParameter("@sex", sex);
SqlParameter sqlparameter3 = new SqlParameter("@permission", permission);
string sort = "z_userid desc";
return SQLHelper.GetDataView(CommandType.Text, procedure, sort, sqlparameter1, sqlparameter2, sqlparameter3);
}
数据库中建立存储过程:
create PROCEDURE zmt_procedure
@z_username nvarchar(20),
@z_sex nvarchar(20),
@z_permission int
AS
BEGIN
DECLARE @sql nvarchar(2000)
set @sql='select u.z_userid,u.z_username,u.z_telephone,u.z_email,s.sex_name,p.permission_name from zmt_user u,zmt_permission p,zmt_sex s where s.sex_id=u.z_sex and p.permission_id = u.z_permission '
if @z_username !='null'
set @sql = @sql+' and u.z_username like ''%'+@z_username+ '%'''
if @z_sex!='2'
set @sql = @sql+' and u.z_sex =''' + @z_sex +''''
if @z_permission!='2'
set @sql = @sql+' and u.z_permission ='+cast(@z_permission as nvarchar)
print (@sql)
execute(@sql) --执行sql字符串
END