2010-11-01 197 views
1

我有一個存儲過程,我用它在.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 

任何想法,爲什麼該參數導致錯誤?

在此先感謝

+1

你得到了什麼錯誤? – 2010-11-01 20:57:35

+0

我假設你在某處設置了shippingCountry = ddlCountries.SelectedValue的字符串值,對吧? – Bill 2010-11-01 22:11:32

+0

是的,我這樣做,並且sql參數會得到正確的選擇,如果從下拉列表中選擇一個值,那麼它的值將是sql null值。 – Laziale 2010-11-02 14:19:40

回答

2

試試這個,我會爲你的所有條款做到這一點。

@ShippingCountry IS NOT NULL AND o.ShippingCountry = @ShippingCountry 

也不能傳遞參數NULL時,你默認的參數NULL的SP。

+0

問題是當我想包括國家參數,我沒有問題,當國家參數不包括在內,只要在標準中包含 – Laziale 2010-11-01 21:50:48

+0

就是這樣,試試我的方式。 – 2010-11-01 22:21:09

+0

我也嘗試過,每當選擇國家時,我仍然沒有收到任何數據:S – Laziale 2010-11-02 14:18:52

0

您是否嘗試過使用一些已知的良好參數值在應用程序之外執行proc?它是否在@ShippingCountry不爲空時返回數據?