C# Gridview搜索

C# Gridview搜索,c#,stored-procedures,gridview,C#,Stored Procedures,Gridview,我创建了一个存储过程,它根据输入搜索gridview,如下所示: Create procedure spSearch ( @Emp_id nvarchar(50) = null, @Emp_name nvarchar(50) = null, @Emp_exp nvarchar(50) = null, @Emp_address nvarchar(50) = null ) AS BEGIN If @Emp_id is not null and Len(@Emp_id )=0 Set @Emp

我创建了一个存储过程,它根据输入搜索gridview,如下所示:

Create procedure spSearch
( 
@Emp_id nvarchar(50) = null,
@Emp_name nvarchar(50) = null,
@Emp_exp nvarchar(50) = null,
@Emp_address nvarchar(50) = null
) 
AS 
BEGIN

If @Emp_id is not null and Len(@Emp_id )=0 Set @Emp_id = null
If @Emp_name is not null and Len(@Emp_name )=0 Set @Emp_name = null
If @Emp_exp is not null and Len(@Emp_exp )=0 Set @Emp_exp = null
If @Emp_address is not null and Len(@Emp_address )=0 Set @Emp_address = null

Select *
From tbl_employee
Where 
(@Emp_id is null or Emp_id Like @Emp_id )
and ( @Emp_name is null or Emp_name Like @Emp_name )
and ( @Emp_exp is null or Emp_exp Like @Emp_exp )
and ( @Emp_address is null or Emp_address Like @Emp_address )
END
但是,只有当输入与已存储的数据完全匹配时,才会返回搜索项

Eg : In tbl_employee I have 
Emp ID = 1
Emp_name = peter
Emp_exp = 2 years
Emp_address = xyz
只有当我输入Emp_name作为“peter”时,搜索结果才会在gridview中填充。 我需要修改存储过程,以便即使在文本框中输入“pe”,也应该填充包含“pe”的所有数据

这是c代码:


如何修改此选项,使搜索更加灵活。

您可以在程序中使用通配符来实现您的要求

请看一下这个


您可以将SQL通配符与LIKE运算符一起使用

and ( @Emp_name is null or Emp_name Like @Emp_name )+ '%'
将为您提供所有以“p”开头的匹配结果。%将替换数据库中所有匹配的字符

select * from yourTable where yourColumn= 'P%'
还建议将搜索字符串大小写发送到存储过程。然后,您还可以在SQL查询中对数据库列进行大写/小写

select * from yourTable where UPPER(yourColumn)= 'P%'
因此,搜索“p%”或“p%”将得到相同的结果

select * from yourTable where UPPER(yourColumn)= 'P%'