【问题标题】:How I can search a field in sql如何在sql中搜索字段
【发布时间】:2016-01-14 09:19:26
【问题描述】:

我有一个包含用户名和密码字段的登录页面。我想在名为“mytable”的表中搜索输入的用户名,如果该表有这样的用户名,则在网格视图中显示他的数据。 带我linq回答

protected void Button1_Click(object sender, EventArgs e)
{
     CostumerDataContext costum = new CostumerDataContext();
     LOGIN2 item = new LOGIN2();
     var islogin = (from u in costum.LOGIN2s 
                         where u.Username == txtUser.Text 
                         && u.Passwrod == txtPass.Text select u).ToList();
     if (islogin.Count>0)
     {
         Dgw.Visible = true;
         Dgw.DataSource = from u in costum.LOGIN2s  select u;
      }
      else
      {
           Label3.Visible=true;
      }
 }

【问题讨论】:

  • 您的意思是您的密码以明文形式存储在数据库表中?哇
  • 那么问题是什么?
  • 它没有正确
  • 你能发布代码有什么问题吗?是不是没有编译,给出意外或错误的输出,运行时失败等等。

标签: asp.net sql-server linq search


【解决方案1】:

您正在基于usernamepassworddatabase 中选择data,但您正在分配whole tablegridview 数据。

您需要将 gridview 与过滤后的数据绑定。

var islogin = (from u in costum.LOGIN2s 
                         where u.Username == txtUser.Text.Trim() 
                         && u.Passwrod == txtPass.Text.Trim() select u).ToList();
if (islogin.Count>0)
{
  Dgw.Visible = true;
  Dgw.DataSource = islogin;
  Dgw.DataBind();
}

else
{
  Label3.Visible=true;
}

【讨论】:

    【解决方案2】:
    protected void Button1_Click(object sender, EventArgs e)
    {
         using (CostumerDataContext costum = new CostumerDataContext ())
         {
    
             LOGIN2 item;
             item  = (from u in costum.LOGIN2s 
                         where u.Username == txtUser.Text 
                         && u.Passwrod == txtPass.Text select u).FirstOrDefault();
            if (item != null)
            {
                Dgw.Visible = true;
                Dgw.DataSource = item;
            }
            else
            {
                Label3.Visible=true;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      相关资源
      最近更新 更多