sql参数导致问题

时间:2010-11-01 20:54:58

标签: .net sql parameters

我有一个存储过程,我在.net程序中填充gridview。

问题是当我填充该参数并将执行发送到数据库时,我没有得到任何回报。

当我在没有该参数的情况下执行查询时,应用程序正在返回数据而没有任何问题。

知道可能出现什么问题?

一些代码:

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

知道该参数导致错误的原因吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

试试这个,我会为你的所有条款做到这一点。

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

当您将参数默认为SP中的NULL时,您也无法在NULL时传递参数。

答案 1 :(得分:0)

您是否尝试使用一些已知良好的参数值来执行应用程序之外的proc?当@ShippingCountry不为空时它是否会返回数据?