简体   繁体   English

将参数的日期类型转换为linq查询中存储过程的varchar

[英]convert the paramater's date type to varchar , of stored procedure in linq query

I have created this stored procedure to filter products by type , category , country , subsidary , date . 我创建了此存储过程,以按typecategorycountrysubsidarydate过滤产品。

 CREATE PROCEDURE [dbo].[FindIncomplete_Products]
     @type nvarchar(250),
     @category nvarchar(250),
     @country nvarchar(250),
     @subsidary nvarchar(250),
     @date datetime
AS
Begin  
    select  [dbo].[AB_Product].[ProductID],
            [dbo].[AB_Product].[ProductTitleEn],
            [dbo].[AB_Product].[ProductTitleAr], 
            [dbo].[AB_Product].[Status],
            [dbo].[AB_ProductType].[ProductTypeNameEn],
            [dbo].[AB_ProductType].[ProductTypeNameAr], 
            [dbo].[AB_ProductTypeCategory].[ProductCategoryNameEn],
            [dbo].[AB_ProductTypeCategory].[ProductCategoryNameAr],
            [dbo].[AB_Subsidary].[SubsidaryNameEn],
            [dbo].[AB_Subsidary].[SubsidaryNameAr],
            [dbo].[AB_Subsidary].[Country]
    from 
        [dbo].[AB_Product]
    inner join 
        [dbo].[AB_ProductType] on [dbo].[AB_ProductType].[ProductTypeID] = [dbo].[AB_Product].[ProductTypeID]
    inner join 
        [dbo].[AB_ProductTypeCategory] on [dbo].[AB_ProductTypeCategory].[ProductCategoryID] = [dbo].[AB_Product].[ProductCategoryID]
    inner join 
        [dbo].[AB_Subsidary] on [dbo].[AB_Subsidary].[SubsidaryID] = [dbo].[AB_Product].[Subsidary_ID]
    WHERE 
       (@type IS NULL OR [dbo].[AB_Product].[ProductTypeID]  LIKE '%' +  @type + '%')
       AND (@category IS NULL OR [dbo].[AB_Product].[ProductCategoryID] LIKE '%' +  @category + '%')
       AND (@country IS NULL OR [dbo].[AB_Subsidary].[Country] LIKE '%' +  @country + '%')
       AND (@subsidary IS NULL OR [dbo].[AB_Product].[Subsidary_ID] LIKE '%' +  @subsidary + '%')
       AND (@date IS NULL OR [dbo].[AB_Product].[CreatedDate] LIKE '%' +  @date + '%')
End

I'm passing the parameter values through LINQ query in my C# controller class. 我正在C#控制器类中通过LINQ查询传递参数值。 This is that linq query 这是linq查询

public ActionResult Program_Search(string type, string category, string country, string subsidary, DateTime? date)
{
     var incomplete_product_result = db.Database.SqlQuery<ProductCollection>("FindIncomplete_Products  @type = {0}, @category = {1}, @country = {2}, @subsidary = {3}, @date = {4}", type, category, country, subsidary, date).ToList();

    return View(incomplete_product_result);
}

Once try to pass parameters by binding with URL like follow 一旦尝试通过绑定URL传递参数,如下所示

http://localhost:49669/Home/Program_Search?type=002&category=null&subsidary=null&country=null&date=12/12/1889

I'm getting following error 我收到以下错误

Conversion failed when converting date and/or time from character string. 从字符串转换日期和/或时间时转换失败。

要解决“数据无法显示”的问题,请检查模型是否存在incomplete_product_result ,该模型当前未绑定到任何模型对象。

Open SSMS (SQL Server Management Studio) and type 打开SSMS(SQL Server Management Studio)并输入

EXEC FindIncomplete_Products NULL,NULL,NULL,NULL,'2015-01-01' 

and press F5 and you will get that error, without anything to do with the web front end 然后按F5键,您将得到该错误,与Web前端无关

This issue is this expression: 这个问题是这个表达:

'%' +  @date + '%'

You are concatenating a string to a date so it tries to implicitly cast the string to a date, and it certainly cant convert a % to a date. 您正在将字符串连接到日期,以便它尝试将字符串隐式转换为日期,并且它肯定无法将%转换为日期。

To fix it, change this line 要解决此问题,请更改此行

AND (@date IS NULL OR [dbo].[AB_Product].[CreatedDate] LIKE '%' +  @date + '%')

To this 对此

AND (@date IS NULL OR [dbo].[AB_Product].[CreatedDate] = @date)

in your stored proc 在您存储的过程中

The first line doesn't actually make logical sense. 第一行实际上没有逻辑意义。 You can't LIKE against a date. 您不能LIKE日期。

This assumes that there are no further issues in the web code. 假设Web代码中没有其他问题。

How did you create the stored proc if you don't know how to use SSMS? 如果您不知道如何使用SSMS,如何创建存储的proc?

(This is actually all based on @Bhavek's comment - he noticed it first!) (这实际上全部基于@Bhavek的评论-他首先注意到了!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM