0

我有 3 个文本框 - 名称、FromDate 和 StartDate。当我在第一个文本框中输入名称并单击按钮时,它应该显示对应于该记录的记录。如果我输入从日期到日期,它应该显示其中包含出生日期的记录输入值的范围。名称搜索正在工作。但是基于日期的搜索不起作用..我想要在 asp.net 中的代码。请帮助...

ALTER PROCEDURE [dbo].[usp_GetSearchResume]                          
@Name varchar(50)=''      
,@FromDate varchar(15)='',                                          
@ToDate varchar (15)=''                                          
AS                          
BEGIN                          
DECLARE @SQL AS VARCHAR(8000) 
SET @SQL = 'SELECT Name,Address,Gender,Marital_Status,Email_Id,Country,State,District,Current_Salary,Total_Salary,Experience,Current_Position,                     
convert(varchar(15),DOB,101) as DOB from Resume'
if(@Name <> '')                          
BEGIN                          
SET @SQL = @SQL + ' where Name like ''%'+  @Name + '%'''                          
END   

IF(@FromDate<>'' and @ToDate<>'' )                                          
begin                                          
set @SQL=@SQL+'or DOB between '''+ CONVERT(varchar(12),@FromDate,101) +''' and ''+  CONVERT(varchar(12),@ToDate,101) +''' '                                               
End                       
print @SQL        
--select (@SQL)                          
exec (@SQL)                          
END  

如果我不输入姓名并且我输入日期和日期,它应该显示基于该日期的记录...请帮助

4

4 回答 4

0

请通过以下方法减少动态字符串查询问题。

它将查找过去 10 天的记录

declare @fromDate datetime = GETDATE()-10
declare @ToDate datetime = GETDATE()

where 1= case   when @fromDate IS NOT NULL AND @ToDate IS NULL AND CreatedOn >
@fromDate  THEN 1 
            when @fromDate IS NULL AND @ToDate IS NOT NULL AND CreatedOn <   @ToDate  THEN 1 
            when CreatedOn between @fromDate AND @ToDate THEN 1 end
于 2012-12-27T09:54:55.873 回答
0

aspx 页面上的代码如下:

</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server">
    <SelectParameters>
     <asp:ControlParameter ControlID="txtName" DefaultValue="%" Name="Name" PropertyName="Text" />
     <asp:ControlParameter ControlID="txtFromDate " DefaultValue="%" Name="FromDate" PropertyName="Text"/>
     <asp:ControlParameter ControlID="txtToDate" DefaultValue="%" Name="txtToDate" PropertyName="Text"/>
    </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
    </asp:GridView>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>

按钮点击事件的代码如下:

 protected void Button1_Click(object sender, EventArgs e)
  {
    string name = txtName.Text;
    string fromdate = txtFromDate.Text;
    string todate = txtToDate.Text;

    SqlDataSource1.SelectParameters.Clear();
    SqlDataSource1.SelectParameters.Add("@Name", name);
    SqlDataSource1.SelectParameters.Add("@FromDate" , fromdate);
    SqlDataSource1.SelectParameters.Add("@ToDate", todate);
    GridView1.Visible = true;
 }
于 2012-12-27T10:01:14.417 回答
0

仅在文本框不为空时使用以下修改后的存储过程并添加参数。

ALTER PROCEDURE [dbo].[usp_GetSearchResume]                          
@Name varchar(50)=NULL     
,@FromDate datetime=NULL,                                          
@ToDate datetime =NULL                                          
AS                          
BEGIN                         


SELECT  Name,Address,Gender,Marital_Status,Email_Id,Country,State,
        District,Current_Salary,Total_Salary,Experience,Current_Position,                     
        convert(varchar(15),DOB,101) as DOB from Resume
where Name like  '%'+ISNULL(@Name,Name)+'%' 
or DOB between  ISNULL(@FromDate,DOB) and  ISNULL(@ToDate,DOB)

END

.cs 页面

 SqlConnection con = new SqlConnection(path);
            SqlCommand cmd = new SqlCommand("usp_GetSearchResume", con);

        cmd.CommandType = CommandType.StoredProcedure; 


        if (!string.IsNullOrWhiteSpace(txtName.Text))
        {
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtNames.Text; 
        }
        if (!string.IsNullOrWhiteSpace(txtFrom.Text))
        {
            DateTime str1 = Convert.ToDateTime(txtFrom.Text);
            cmd.Parameters.Add("@FromDate", SqlDbType.DateTime).Value = str1;
        }
        if (!string.IsNullOrWhiteSpace(txtTo.Text))
        {
             DateTime str2 = Convert.ToDateTime(txtTo.Text);           
             cmd.Parameters.Add("@ToDate", SqlDbType.DateTime).Value = str2;
        }
于 2012-12-27T10:25:22.770 回答
0

更改您的存储过程,如下所示......

Alter PROCEDURE [dbo].[usp_GetSearchResume]         
@Name varchar(50)='',
@FromDate varchar(15)='',
@ToDate varchar (15)=''      
AS   BEGIN 
DECLARE @SQL AS VARCHAR(8000)  
SET @SQL = 'SELECT Name,Address,Gender,Marital_Status,Email_Id,Country,State,District,Current_Salary,Total_Salary,Experience,Current_Position, convert(varchar(15),DOB,101) as DOB from Resume' 
if(@Name <> '')    
 BEGIN            
           SET @SQL = @SQL + ' where Name like ''%'+  @Name + '%'''                           END   

IF(@FromDate<>'' and @ToDate<>'' )                                   
begin        
  if(@Name <> '')    
    BEGIN           
      set @SQL=@SQL+'or convert(varchar(15),DOB,101) between '''+ @FromDate
+''' and ''' +  @ToDate +''' ' 
    END    
  ELSE 
    BEGIN  
      set @SQL=@SQL+' Where convert(varchar(15),DOB,101) between '''+ @FromDate +''' and ''' +  @ToDate +''' '  
    END     
End                        
print @SQL        
--select (@SQL)          
exec (@SQL)                   
END
于 2012-12-27T10:25:33.590 回答