Linq 笔记

查询
  if (this.name.Text.Trim() == null || this.name.Text.Trim() == "")
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('用户名不能为空!');</script>");
            this.name.Focus();
            return;
            
        }
        if (this.password.Text.Trim() == null || this.password.Text.Trim() == "")
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('密码不能为空');</script>");
            this.password.Focus();
            return;
        }
        try
        {
            NovelDataContext ndc = new NovelDataContext();
            string name = this.name.Text.Trim();
            string password = this.password.Text.Trim();
            var denglu = from lg in ndc.login
                         where lg.name == name && lg.password == password
                         select lg;            
            if (denglu.Count() > 0)
            {               
                Response.Cookies["UserName"].Value = this.name.Text.Trim();
                if (this.CheckBox1.Checked)                {
                   
                    Response.Cookies["UserName"].Expires = DateTime.Today.AddDays(12);
                }
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('恭喜您,登录成功!');window.location='Default.aspx'</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('登录失败,用户名或密码错误!');</script>");
            }
        }
        catch (Exception)
        {
            
             Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('登录失败,请联系管理员!');</script>");
        }
      
    }


插入
 NovelDataContext ndc = new NovelDataContext();
                login lg = new login()
                {
                    name = name.Text.Trim(),
                    password = password.Text.Trim(),
                    tpassword = TextBox1.Text.Trim()
                };
                ndc.login.InsertOnSubmit(lg);
                ndc.SubmitChanges();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('恭喜您,注册成功!');window.location='login.aspx'</script>");

修改
 NovelDataContext ndc = new NovelDataContext();
                string name = this.name.Text.Trim();
                string tpassword = this.TextBox1.Text.Trim();
                string password = this.password.Text.Trim();              
                var SelectUser = from lg in ndc.login
                                 where lg.name == name && lg.tpassword == tpassword
                                 select lg;

                if (SelectUser.Count() > 0)
                {
                    foreach (var su in SelectUser)
                    {
                        su.password = password;
                    }
                    ndc.SubmitChanges();

删除
NovelDataContext ndc = new NovelDataContext();
                string name = this.name.Text.Trim();
                string tpassword = this.TextBox1.Text.Trim();
                string password = this.password.Text.Trim();              
                var SelectUser = from lg in ndc.login
                                 where lg.name == name && lg.tpassword == tpassword
                                 select lg;

                if (SelectUser.Count() > 0)
                {
               
                    ndc.login.DeleteAllOnSubmit(SelectUser);
                    ndc.SubmitChanges();
                  }



你可能感兴趣的:(LINQ)