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

Change this:

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

To this:

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

Then change this:

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

to this:

// 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;

You can also shorten your parameters to this:

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);

And one more thing, change this @TrainingCost VARCHAR(50) = null, to @TrainingCost NUMERIC(18,6) = null for example.

And then change this:

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

To:

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

And then change this too:

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

Or this:

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

Accordingly, change your stored proc too, this look unusual, a Cost field is money; money should not be handled as string. Convert this:

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

To this:

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

Likewise, if you need null, properly handle it:

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 に答える