1

我在使用日期字段搜索数据时遇到问题。

我有一个正在执行搜索的存储过程:

ALTER PROCEDURE [dbo].[spSearchTraining]
(
  @CourseName VARCHAR(50)= null,
  @TrainingProvider VARCHAR(50)= null,
  @TrainingCost VARCHAR(50) = null,
  @StartDate Varchar(50) = null,
  @EndDate Varchar(50)= null,
  @RowCount int output

)
AS
 BEGIN

  SELECT *  FROM vwTrainingDetails 
  WHERE ( CourseName Like '%' + @CourseName +'%' or @CourseName is null)
  AND ( TrainingProvider Like '%' + @TrainingProvider + '%' or @TrainingProvider is null)
  AND ( Cost Like '%' +  @TrainingCost +'%'  or @TrainingCost is null)
  AND ([Start Date] >=  @StartDate or @StartDate is null)
  AND ([Start Date] <=  @EndDate  or @EndDate is null)


  select @RowCount=@@ROWCOUNT

END

我传递我的参数如下:

//Create and add a parameter to Parameters collection for the stored procedure.
        MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@CourseName", SqlDbType.VarChar, 40));
        MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@TrainingProvider", SqlDbType.VarChar, 40));
        MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@TrainingCost", SqlDbType.VarChar, 40));
        MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.VarChar, 40));
        MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.VarChar, 40));


        //string mysdate = (StartTextBox.Text).ToString;
        //Assign the search value to the parameter.
        MyDataAdapter.SelectCommand.Parameters["@CourseName"].Value = (CourseTextBox.Text).Trim();
        MyDataAdapter.SelectCommand.Parameters["@TrainingProvider"].Value = (ProviderTextBox.Text).Trim();
        MyDataAdapter.SelectCommand.Parameters["@TrainingCost"].Value = (CostTextBox.Text).Trim();
        MyDataAdapter.SelectCommand.Parameters["@StartDate"].Value = (StartTextBox.Text);
        MyDataAdapter.SelectCommand.Parameters["@EndDate"].Value =(EndTextBox.Text);

我遇到的问题是,当我按原样运行时,存储过程不会返回任何内容。如果我注释掉:

MyDataAdapter.SelectCommand.Parameters["@StartDate"].Value = (StartTextBox.Text);
MyDataAdapter.SelectCommand.Parameters["@EndDate"].Value =(EndTextBox.Text);

然后它可以工作,但无法使用日期进行搜索。

当我从服务器执行存储过程并传入包括日期在内的参数时,它工作正常。

有人可以帮我看看我做错了什么!!!?

谢谢

4

3 回答 3

0

如果您使用的是 SQL 2008,那么您可以使用 CAST 函数将字符串类型的参数 @StartDate 和 @EndDate 转换为 Date

AND ([Start Date] >=  CAST(@StartDate AS DATE) or @StartDate is null)
AND ([Start Date] <=  CAST(@EndDate  AS DATE) or @EndDate is null)
于 2012-04-18T16:41:28.373 回答
0

改变这个:

@StartDate Varchar(50) = null,
@EndDate Varchar(50)= null,

对此:

@StartDate DATE = null,
@EndDate DATE = null,

然后改变这个:

MyDataAdapter.SelectCommand.Parameters.Add(
    new SqlParameter("@StartDate", SqlDbType.Date));
MyDataAdapter.SelectCommand.Parameters.Add(
    new SqlParameter("@EndDate", SqlDbType.Date));

对此:

// I don't know the exact format of your date, 
// fill-in this accordingly from your Substring'd textboxes:
int startYear = int.Parse(...); 
int startMonth = int.Parse(...); 
int startDay = int.Parse(...); 

int endYear = int.Parse(...); 
int endMonth = int.Parse(...); 
int endDay = int.Parse(...); 



DateTime startDate = new DateTime(startYear,startMonth,startDay);
DateTime endDate = new DateTime(endYear,endMonth,endDay);

MyDataAdapter.SelectCommand.Parameters["@StartDate"].Value = startDate;
MyDataAdapter.SelectCommand.Parameters["@EndDate"].Value = endDate;

您还可以将参数缩短为:

MyDataAdapter.SelectCommand.Parameters.AddWithValue("@CourseName",
    CourseTextBox.Text.Trim());
MyDataAdapter.SelectCommand.Parameters.AddWithValue("@TrainingProvider", 
    ProviderTextBox.Text.Trim());        
MyDataAdapter.SelectCommand.Parameters.AddWithValue("@TrainingCost", 
    CostTextBox.Text.Trim());

MyDataAdapter.SelectCommand.Parameters.AddWithValue("@StartDate", startDate);
MyDataAdapter.SelectCommand.Parameters.AddWithValue("@EndDate", endDate);

还有一件事,例如将其更改@TrainingCost VARCHAR(50) = null,@TrainingCost NUMERIC(18,6) = null

然后改变这个:

MyDataAdapter.SelectCommand.Parameters.Add(
   new SqlParameter("@TrainingCost", SqlDbType.Varchar, 40));

至:

MyDataAdapter.SelectCommand.Parameters.Add(
   new SqlParameter("@TrainingCost", SqlDbType.Decimal));

然后也改变它:

MyDataAdapter.SelectCommand.Parameters["@TrainingCost"].Value = 
     decimal.Parse(CostTextBox.Text.Trim());

或这个:

MyDataAdapter.SelectCommand.Parameters.AddWithValue("@TrainingCost", 
    decimal.Parse(CostTextBox.Text.Trim()));

因此,也改变你的存储过程,这看起来很不寻常,Cost 字段是钱;钱不应该作为字符串处理。转换这个:

AND ( Cost Like '%' +  @TrainingCost +'%'  or @TrainingCost is null)

对此:

AND (Cost = @TrainingCost or @TrainingCost is null)

同样,如果您需要 null,请正确处理它:

MyDataAdapter.SelectCommand.Parameters["@TrainingCost"].Value = 
     CostTextBox.Text.Trim().Length == 0 ? 
       (decimal?) null :
       (decimal?) decimal.Parse(CostTextBox.Text.Trim());
于 2012-05-21T12:57:24.177 回答
0

在您的 SP 中使用以下查询

SELECT *  
FROM vwTrainingDetails    
WHERE ( CourseName Like '%' + ISNULL(@CourseName,CourseName) +'%')
AND ( TrainingProvider Like '%' + ISNULL(@TrainingProvider, TrainingProvider) + '%')
AND ( Cost Like '%' +  ISNULL(@TrainingCost,Cost) +'%')
AND ([Start Date] >=  ISNULL( @StartDate, [Start Date]))
AND ([Start Date] <=  ISNULL(@EndDate, [Start Date]))
于 2012-05-21T12:27:42.023 回答