我在使用日期字段搜索数据时遇到问题。
我有一个正在执行搜索的存储过程:
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);
然后它可以工作,但无法使用日期进行搜索。
当我从服务器执行存储过程并传入包括日期在内的参数时,它工作正常。
有人可以帮我看看我做错了什么!!!?
谢谢