我在使用日期字段搜索数据时遇到问题。
我有一个执行搜索的存储过程:
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);
然后它有效但无法使用日期进行搜索。
当我从服务器执行存储过程并传入包含日期的参数时,它可以正常工作。
有人可以帮助我,看看我做错了什么!!!?
谢谢
答案 0 :(得分: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)
答案 1 :(得分: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]))
答案 2 :(得分: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()));
因此,改变您的存储过程,这看起来不寻常,成本领域是金钱;钱不应该作为字符串处理。转换这个:
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());