【问题标题】:Sending null parameter to querystring Stored Procedure将空参数发送到查询字符串存储过程
【发布时间】:2013-07-11 15:06:32
【问题描述】:

我有以下问题。我想使用查询字符串参数向存储过程发送空值。

这是我的存储过程

CREATE PROCEDURE spGetAllProducts
 @catID int = null,
 @subcatID int = null
AS
BEGIN
    SELECT * FROM Products WHERE
    ((@catID is null) OR (category_id = @catID))
    AND ((@subcatID is null) OR (subcategory_id = @subcatID))
END

当我按照我想要的方式测试以下查询时它正在工作

spGetAllProducts null, '3'
spGetAllProducts '8', null
spGetAllProducts '8', '3'
spGetAllProducts null, null

但是当我尝试使用查询参数发送空值时它不起作用

这是我的 sqldatasource

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringReviewDB %>" SelectCommandType="StoredProcedure" SelectCommand="spGetAllProducts">

    <SelectParameters>
    <asp:QueryStringParameter Direction="Input" Name="catID" QueryStringField="catID" Type="Int32" />
    <asp:QueryStringParameter Direction="Input" Name="subcatID" QueryStringField="subcatID" Type="Int32" DefaultValue="" ConvertEmptyStringToNull="True" />
    </SelectParameters>

    </asp:SqlDataSource>

当我发送这样的查询时

http://localhost:50693/TreeViews.aspx?catID=8&subcatID=3

它正在工作,但是当我发送以下查询时它不工作

http://localhost:50693/TreeViews.aspx?catID=8&subcatID=

谢了

【问题讨论】:

  • @LearningAsp 回答,您的第二个查询等于以下内容:spGetAllProducts '8', ''

标签: c# asp.net stored-procedures sql-server-2012-express


【解决方案1】:

这样试试

CREATE PROCEDURE spGetAllProducts @catID int = null, @subcatID int = null AS BEGIN SELECT * FROM Products WHERE ((@catID '') OR (category_id = @catID)) AND ((@subcatID = '') OR (subcategory_id = @subcatID)) END

我认为由于存储过程将它作为空字符串而不是 null ,我有类似的问题,它对我有用

【讨论】:

  • thx 我现在更改了我的存储过程,当我发送“0”值时它正在工作
  • 好的,然后在发送到存储过程时检查您的参数,如果是“”即空字符串,则将其替换为0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多