0

嗨,我正在尝试创建一个搜索 ListView 的搜索框,我设法做到了这一点,如果我让它只由用户名搜索,它就可以工作。只要我用参数替换用户名,代码就不再工作了。这里是我的代码:

string searchSelectBox = SearchSelectDropDown.SelectedItem.Text;

GetAllUsersFromDb.SelectCommand =
    "SELECT aspnet_Users.UserName, aspnet_Roles.RoleName, "
    + "aspnet_Membership.Email, aspnet_Membership.LastLoginDate "
    + "FROM aspnet_Users "
    + "INNER JOIN aspnet_UsersInRoles "
    + "ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId "
    + "INNER JOIN aspnet_Roles "
    + "ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId "
    + "INNER JOIN aspnet_Membership "
    + "ON aspnet_Users.UserId = aspnet_Membership.UserId "
    + "WHERE @SelectField LiKE @Param";
GetAllUsersFromDb.SelectParameters.Clear();
GetAllUsersFromDb.SelectParameters.Add("SelectField" , searchSelectBox);
GetAllUsersFromDb.SelectParameters.Add("Param" , SearchBox.Text);
GetAllUsersFromDb.DataBind();
textListView.DataBind();    

searchSelectBox 可以采用的值是 Username 、 Rolename 和 Email

如果我用用户名替换@SelectField 并输入一个正确的用户名,则搜索可以工作,但实际上它没有。

这里有什么问题?

4

1 回答 1

2

试试这个

string searchSelectBox = SearchSelectDropDown.SelectedItem.Text;

    GetAllUsersFromDb.SelectCommand =
       string.Format("SELECT aspnet_Users.UserName, aspnet_Roles.RoleName, 
                      aspnet_Membership.Email, aspnet_Membership.LastLoginDate 
                      FROM aspnet_Users 
                      INNER JOIN aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId 
                      INNER JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId 
                      INNER JOIN aspnet_Membership ON aspnet_Users.UserId = aspnet_Membership.UserId 
                      WHERE {0} LiKE @Param", searchSelectBox);
    GetAllUsersFromDb.SelectParameters.Clear();

    GetAllUsersFromDb.SelectParameters.Add("Param" , SearchBox.Text);
    GetAllUsersFromDb.DataBind();
    textListView.DataBind(); 
于 2012-11-28T21:18:39.260 回答