为什么以下仅在设置两个参数时才返回数据?

时间:2009-03-27 18:10:09

标签: c# asp.net sqldatasource

如果我在Sql Management Studio中运行SearchMaster和SearchDetails这两个程序,当我在设计视图中设置存储过程时测试查询时,SearchMaster运行正常,但是当我运行它时,没有行是如果我只输入frn或business的信息,我就会创建。如果我输入两者,如果匹配,我会得到数据。参数etup是否正确?另外,如果我在过程中将参数初始化为null,是否仍然需要使用ConvertEmptyStringToNull?

这是SearchMaster StoredProcedure:

CREAETE PROCEDURE SearchMaster
@business nvarchar(300) = NULL,
@frn nvarchar(10) = NULL
AS
SELECT h.Business,
       hrl.frn
FROM registration hrl
INNER JOIN holder h on h.call = hrl.call
WHERE (@business IS NULL OR h.Business like '%' + @business + '%') 
AND (@frn IS NULL OR hrl.frn = @frn)

这是SearchDetails StoredProcedure:

CREATE PROCEDURE SearchDetails
@business nvarchar(300) = NULL,
@frn nvarchar(10) = NULL
AS
SELECT hrl.call 
FROM registration hrl 
INNER JOIN holder h ON h.call = hrl.call
WHERE (@business IS NULL OR h.Business LIKE '%' + @business + '%') 
  AND (@frn IS NULL OR hrl.frn = @frn)

以下是SearchMaster过程的SqlDataSource:

<asp:SqlDataSource ID="sqlDsDetails" 
               runat="server" 
               ConnectionString="<%$ ConnectionStrings:cnxString %>
               SelectCommandType="StoredProcedure" 
               SelectCommand="SearchMaster">
<SelectParameters>
<asp:ControlParameter Name="business" ControlID="txtBusiness" 
                  Type="String" PropertyName="Text"  
                  ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="frn" ControlID="txtFRN" 
                  Type="String" PropertyName="Text"
                  ConvertEmptyStringToNull="true"/>
</SelectParameters>
</asp:SqlDataSource>

以下是SearchDetails过程的SqlDataSource:

<asp:SqlDataSource ID="sqlDsDetails" 
               runat="server" 
               ConnectionString="<%$ ConnectionStrings:cnxString %>
               SelectCommandType="StoredProcedure" 
               SelectCommand="SearchDetails">
<SelectParameters>
<asp:Parameter Name="frn" Type="String" DefaultValue="" 
           ConvertEmptyStringToNull="true" />
<asp:Parameter Name="business" Type="String" DefaultValue="" 
           ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>

这是绑定SqlDsMaster的按钮点击:

protected void btnSearch_Click(object sender, EventArgs e)
{
    sqlDsMaster.DataBind();
}

这是gvMaster_RowCreated,用于创建详细信息的行:

protected void gvMaster_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        SqlDataSource ctrl = 
        e.Row.FindControl("sqlDsDetails") as SqlDataSource;

        if (ctrl != null && e.Row.DataItem != null)
        {
            ctrl.SelectParameters["frn"].DefaultValue = 
            ((DataRowView)e.Row.DataItem)["frn"].ToString();

            ctrl.SelectParameters["business"].DefaultValue = 
            ((DataRowView)e.Row.DataItem)["business"].ToString();
         }
     }
 }

我运行了SQL事件探查器,当只输入一个参数时,探查器甚至没有显示该过程正在运行。当我输入两个参数时,程序显示在分析器中运行。

2 个答案:

答案 0 :(得分:2)

我的猜测是它仍然传入一个空字符串,用于你认为是空的情况。您可以通过以下方式排除这种情况:

CREATE PROCEDURE SearchDetails
@business nvarchar(300) = NULL,
@frn nvarchar(10) = NULL
AS

SELECT @business = NULLIF(@business, ''),
       @frn = NULLIF(@frn, '')

 SELECT hrl.call // etc

如果它开始工作,那就是传入空字符串。

答案 1 :(得分:1)

我怀疑Marc Gravell是对的。

尝试以下测试:

 SearchMaster @business = null        , @frn = null
 SearchMaster @business = 'something' , @frn = null
 SearchMaster @business = 'something' , @frn = 'something'
 SearchMaster @business = null        , @frn = 'something'
 SearchMaster @business = 'something' , @frn = 'something'
 SearchMaster @business = 'something' , @frn = ''
 SearchMaster @business = ''          , @frn = 'something'

请注意,最后2个不会返回任何内容 - 除非您在数据库中有空白的业务和frn;)

如果你把Marc中的select语句放在AS语句后面,那么它应该按照你的预期运行。

这样的事情:

CREATE PROCEDURE SearchMaster
@business nvarchar(300) = NULL,
@frn nvarchar(10) = NULL
AS
SELECT @business = NULLIF(@business,'') , @frn = NULLIF(@frn,'')

SELECT h.Business,
       hrl.frn
FROM registration hrl
INNER JOIN holder h on h.call = hrl.call
WHERE (@business IS NULL OR h.Business like '%' + @business + '%') 
AND (@frn IS NULL OR hrl.frn = @frn)

并确保在您的数据源上设置CancelSelectOnNullParameter =“False”