我们对在 Visual Studio 数据集设计器中创建的强类型数据集进行了以下查询:
SELECT ID, PaymentAmount, PaymentDate, WhatWasPaymentFor
FROM Payments
WHERE ParentID = @ParentID
AND (@PaymentDate is null OR PaymentDate = @PaymentDate)
AND (@PaymentAmount is null OR PaymentAmount = @PaymentAmount)
AND ((@SearchValue is null OR
(WhatWasPaymentFor LIKE '%' + @SearchValue + '%' OR @SearchValue='ALL'))
)
在 VB.Net 代码隐藏文件中,我们调用查询来填充 DataSet,如下所示:
Dim tblObject = theTableAdapter.GetDataByAllInOne(dcmParentsId, Nothing, Nothing, TextBoxSearch.Text)
当我认为它是字符串时,Intellisense 将第 4 个参数显示为十进制。当鼠标悬停在@SearchValue 的参数上时,我发现了这一点。
执行此编码时,将显示此错误:
Conversion from string "Books" to type 'Decimal' is not valid.
此查询中唯一的十进制列是 PaymentAmount,它恰好是第三个参数而不是第四个参数。
我不确定为什么智能感知指出第四个参数 @SearchValue 是十进制。您能告诉我如何将其更改为字符串吗?
确实发生的一件有趣的事情是,如果在 SQL Server Magagement Studio 中运行,查询将正常工作:
DECLARE @SearchValue VARCHAR = 'Books'
DECLARE @ParentID INT = 3
DECLARE @PaymentDate DATETIME = NULL
DECLARE @PaymentAmount MONEY = NULL
SELECT ID, PaymentAmount, PaymentDate, WhatWasPaymentFor
FROM Payments
WHERE ParentID = @ParentID
AND (@PaymentDate is null OR PaymentDate = @PaymentDate)
AND (@PaymentAmount is null OR PaymentAmount = @PaymentAmount)
AND ((@SearchValue is null OR
(WhatWasPaymentFor LIKE '%' + @SearchValue + '%' OR @SearchValue='ALL'))
)
也许在我不知道的 DataSet 设计器中需要设置一些东西。