我有一個存儲過程,我用它在.net程序中填充gridview。SQL參數導致問題
問題是,當我填充該參數,併發送執行到數據庫,我沒有得到任何回報。
當我執行沒有該參數的查詢時,那麼應用程序返回的數據沒有任何問題。
任何想法可能是什麼問題?
一些代碼:
if (ddlCountries.SelectedIndex > 0)
{
commAdvanced.Parameters.Add("@ShippingCountry", SqlDbType.NVarChar).Value = shippingCountry;
}
else
{
commAdvanced.Parameters.Add("@ShippingCountry", SqlDbType.NVarChar).Value = DBNull.Value;
}
,這裏是存儲過程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE Dbo_SearchAll
(
@StartTime datetime = null,
@EndTime datetime = null,
@CustomerEmail nvarchar(255) = null,
@OrderStatusID nvarchar = null,
@PaymentStatusID nvarchar = null,
@Username nvarchar(255) = null,
@CustomerName nvarchar(255) = null,
@OrderNumber int = null,
@MinimumOrderAmount decimal = null,
@MaximumOrderAmount decimal = null,
@ShippingMethod nvarchar = null,
@SKU nvarchar(255) = null,
@CouponID int = null,
@DiscountType int = null,
@ShippingCountry nvarchar = null
)
AS BEGIN
SET NOCOUNT ON
SELECT DISTINCT
o.OrderID, o.OrderTotal, o.ShippingCountry,
n.Name AS OrderStatus,
p.Name AS PaymentStatus
FROM
Order o
JOIN
OrderStatus n ON o.OrderStatusID = n.OrderStatusID
JOIN
PaymentStatus p ON o.OrderStatusID = p.PaymentStatusID
JOIN
Customer c ON o.CustomerID = c.CustomerID
JOIN
OrderVariationsPeople op ON o.OrderID = op.OrderID
JOIN
VariousPeople pv ON op.ProductID = pv.ProductId
WHERE
(o.CreatedOn > @StartTime OR @StartTime IS NULL)
AND (o.CreatedOn < @EndTime OR @EndTime IS NULL)
AND (o.ShippingEmail = @CustomerEmail OR @CustomerEmail IS NULL)
AND (o.OrderStatusID IN (@OrderStatusID) OR @OrderStatusID IS NULL)
AND (o.PaymentStatusID IN (@PaymentStatusID) OR @PaymentStatusID IS NULL)
AND (c.Username = @Username OR @Username IS NULL)
AND (o.BillingFirstName + ' ' + o.BillingLastName = @CustomerName OR @CustomerName IS NULL)
AND (o.ShippingFirstName + ' ' + o.ShippingLastName = @CustomerName OR @CustomerName IS NULL)
AND (o.OrderID = @OrderNumber OR @OrderNumber IS NULL)
AND (o.OrderTotal > @MinimumOrderAmount or @MinimumOrderAmount IS NULL)
AND (o.OrderTotal < @MaximumOrderAmount OR @MaximumOrderAmount IS NULL)
AND (o.ShippingMethod = @ShippingMethod OR @ShippingMethod IS NULL)
AND (pv.SKU = @SKU OR @SKU IS NULL)
AND (o.DiscountID = @DiscountType OR @DiscountType IS NULL)
AND (o.ShippingCountry = @ShippingCountry OR @ShippingCountry IS NULL)
ORDER BY
o.OrderID
END
任何想法,爲什麼該參數導致錯誤?
在此先感謝
你得到了什麼錯誤? – 2010-11-01 20:57:35
我假設你在某處設置了shippingCountry = ddlCountries.SelectedValue的字符串值,對吧? – Bill 2010-11-01 22:11:32
是的,我這樣做,並且sql參數會得到正確的選擇,如果從下拉列表中選擇一個值,那麼它的值將是sql null值。 – Laziale 2010-11-02 14:19:40