LinQ是什么?
LinQ是语言集成的查询,是用于C#跟Vb的扩展语言
LinQ的用法
新建一个App_Code文件夹,在文件夹下添加一个数据LinQ to SQL类,可以直接直接点击服务器管理器然后就可以连接到数据库,
LinQ的增用法
using(DcDataContext dc =new DcDataContext()) {
//添加数据 dc.Users.InsertOnSubmit(us); dc.SubmitChanges(); }
LinQ的删用法
if (listView1.SelectedItems.Count == 1) { using (DcDataContext con = new DcDataContext()) { List<Users> ulist = con.Users.Where(r => r.Ids.ToString() == listView1.SelectedItems[0].Text).ToList(); if (ulist.Count > 0) { DialogResult d = MessageBox.Show("你确定要删除吗?", "删除", MessageBoxButtons.OKCancel); if (d == DialogResult.OK) { con.Users.DeleteOnSubmit(ulist[0]); con.SubmitChanges(); Sx(); } } } } else { MessageBox.Show("请选择一项删除!"); }
LinQ修改
using (DcDataContext con = new DcDataContext()) { Users u = con.Users.Where(r => r.Ids.ToString() == F2).FirstOrDefault(); if (u != null) { if (textBox1.Text == "") { label7.Text = "用户名不能为空!"; } else { u.UserName = textBox1.Text; } if (textBox2.Text == "") { label8.Text = "密码不能为空!"; } else { if (textBox2.Text == textBox3.Text) { u.Password = textBox2.Text; } else { label8.Text = "密码不一致!"; return; } } u.NickName = textBox3.Text; if (radioButton1.Checked) { u.Sex = true; } else { u.Sex = false; } u.Birthday = dateTimePicker1.Value; u.NationCode = comboBox1.Text; Nation u1 = con.Nation.Where(d => d.NationName == u.NationCode).FirstOrDefault(); if (u1 != null) { u.NationCode = u1.NationCode; } con.SubmitChanges(); MessageBox.Show("修改成功!"); F1.Sx(); this.Close(); } }
LinQ查询
模糊查询
List<Users> u = con.Users.Where(d =>d.NickName.Contains(st)).ToList();
精确查询
List<Users> u = con.Users.Where(d =>d.NickName==s).ToList();