如何根据我在网格中的搜索来获取数据

时间:2011-05-13 05:51:44

标签: c# sql .net sql-server database

在文本框中,我必须输入将在表格中搜索的字符串。如果在3列中的任何一列中找到数据,数据将显示在gridview.3中。列是标题,描述和关键字。如果选中了标题chk框,它将搜索标题,如果检查了关键字,它将搜索它。如果未选中任何复选框,则不会在特定列中搜索。我有2个单选按钮也需要选择其中一个。如果点击and_radio它将搜索两列中的数据如果或被点击idf数据在任何列中都会被显示....帮助尝试它很长...必须用程序完成..我应该在搜索按钮上点击什么代码来搜索文本框中的数据,然后在网格中填充结果 我使用的存储过程

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

 ALTER Procedure [dbo].[sp_Normal_Search_Library]

 @title AS nvarchar(max), 
  @Description AS nvarchar(max), 
  @Keywords AS nvarchar(max),
 @Chk_title AS BIT ,
 @Chk_Description AS Bit ,
 @Chk_Keywords AS BIT,
 @RD_AND AS BIT,
 @RD_OR AS BIT


 AS 
if @RD_AND = 1 Begin 
    if @Chk_title = 1       
Begin       
Select title from server_des where title Like '%'+@title+'%' 
End     
if @Chk_Description=1   
    Begin   
    Select Description from server_des where Description  Like '%'+@Description+'%'
        End     
if @Chk_Keywords=1  
    Begin
        Select Keywords from server_des where Keywords Like '%'+@Keywords+'%'
        End     
if @Chk_title = 1 AND @Chk_Description = 1 
        Begin   
    Select title, Description from server_des where title Like '%'+ @title+'%' AND Description  Like '%'+@Description+'%'
        End 
    if @Chk_Description=1 AND @Chk_Keywords=1   
    Begin       
Select Description, Keywords from server_des where  Description  Like'%'+@Description+'%' AND  Keywords Like '%'+@Keywords+'%'  
    End 
    if @Chk_title=1 AND @Chk_Keywords=1
        Begin   
    Select title, Keywords from server_des where title Like'%'+@title+'%' AND Keywords Like'%'+@Keywords+'%'        
End     
if @Chk_title=1 AND @Description=1 AND @title=1     
    Begin       
Select title,Description, Keywords from server_des where title Like '%'+@title+'%'AND Description Like '%'+@Description+'%' AND Keywords Like '%' +@Keywords+'%'
        End     
 End    
ELSE IF @RD_OR=0    
Begin
if @Chk_title = 1   
    Begin       
Select title from server_des where title Like'%'+ @title+'%'    
    End     
if @Chk_Description=1       
Begin       
Select Description from server_des where Description  Like '%'+@Description+'%'
        End     
if @Chk_Keywords=1  
    Begin
        Select Keywords from server_des where Keywords Like '%'+@Keywords+'%'
        End     
if @Chk_title = 1 AND @Chk_Description = 1  
    Begin   
    Select title, Description from server_des where title Like '%'+ @title+'%' AND Description  Like '%'+@Description+'%'
        End     
if @Chk_Description=1 AND @Chk_Keywords=1       
Begin       
Select Description, Keywords from server_des where  Description  Like '%'+@Description+'%' AND  Keywords Like '%'+@Keywords+'%'     
    End 
    if @Chk_title=1 AND @Chk_Keywords=1
        Begin   
    Select title, Keywords from server_des where title Like '%'+@title+'%' AND Keywords Like '%'+@Keywords+'%'  
    End 
    if @Chk_title=1 AND @Description=1 AND @title=1     
    Begin   
    Select title, Description, Keywords from server_des where title Like'%'+@title+'%'AND  Description Like '%'+@Description+'%' AND Keywords Like '%' +@Keywords+'%'
        End 
     End

在业务逻辑中,我创建了方法:

 public DataTable Fillgrid(string title,string Description, string Keywords)
    {
    con.ConnectionString = ConfigurationManager.ConnectionStrings["LibrarySql"].ConnectionString;
    SqlCommand cmd = new SqlCommand();

    cmd.CommandText = "sp_Normal_Search_Library";
    cmd.CommandType = CommandType.StoredProcedure;
    //string query = "select * from Server_des";
    cmd.Parameters.AddWithValue("@title", title);
    cmd.Parameters.AddWithValue("@Description",Description);
    cmd.Parameters.AddWithValue("@Keywords",Keywords);
    cmd.Connection = con;
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable  dt = new DataTable();
    con.Open();
    sda.Fill(dt);


    return dt;

现在如何在网络表单中填充网格中的数据?

1 个答案:

答案 0 :(得分:0)

DataGrid.DataSource = Fillgrid("title","description","keyword"); DataGrid.Bind();