【问题标题】:SqlDataSource SelectCommand using 2 Parameters and LIKE does not workSqlDataSource SelectCommand 使用 2 个参数和 LIKE 不起作用
【发布时间】:2012-06-19 15:46:39
【问题描述】:

我在 SqlDataSource 中有以下 selectcommand,我的问题是它只在我有两个参数的值时才返回数据。当只有 1 有值时,我还需要它来返回数据。

SelectCommand="SELECT user.UserId, UserProfiles.Company, UserProfiles.Address, UserProfiles.JV2, UserProfiles.DB1, aspnet_Membership.Email, user.UserName
FROM user INNER JOIN UserProfiles ON user.UserId = UserProfiles.UserId  WHERE        (@UserName  IS NULL OR @UserName = ''  OR user.UserName LIKE '%'+@UserName+'%' ) AND
                     (@Email IS NULL  OR @Email = '' OR user.Email LIKE '%'+@Email+'%') 

使用以下参数

   <SelectParameters>  
    <asp:ControlParameter Name="Email" ControlID="eMail"  PropertyName="Text" Type="String"  />
<asp:ControlParameter Name="UserName" ControlID="userName"  PropertyName="Text" Type="String"  />

当我在 SQL Server Management Studio 中运行查询时,它会在我只输入 1 个值或两者时返回数据。 当我运行表单时,只有在两个字段中输入值时才会返回数据。

我发现了这篇文章:SqlDataSource SelectCommand using LIKE does not work,这很相似,但解决方案没有帮助。

显然我做错了什么,我只是看不出是什么。

【问题讨论】:

    标签: asp.net sql sqldatasource


    【解决方案1】:

    我遇到了类似的问题,将其添加到 sqldatasource 中修复了它:

    <asp:SqlDataSource CancelSelectOnNullParameter="false">
    

    【讨论】:

      猜你喜欢
      • 2010-10-15
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多