标签:
LINQ:语言集成查询
帮我们干了两件事:1.帮我们造了实体对象出来;
2.提供了很多便捷的增删改查询方法,将数据库的T-sql挪了过来。
右键添加LINQ类之后,右击服务器资源管理器中的数据连接,添加链接,选择数据库,完成连接。
将资源管理器中的表格拖进类文件里就可以创建表格的实体对象,以后可以直接使用
private void Form1_Load(object sender, EventArgs e) { DataClasses1DataContext context = new DataClasses1DataContext(); List<Car >list=context.Car.ToList(); dataGridView1.DataSource = list; }
运行结果
//Func<string, string> fc = delegate(string a) //{ // a += "你好"; // return a; //}; //MessageBox.Show(fc("5").ToString()); DataClasses1DataContext context = new DataClasses1DataContext(); //List<bread> list = context.bread.Where(r => Convert.ToDecimal(r.breadprice.ToString()) < 15).ToList(); var list2 = from quary in context.bread where Convert.ToDecimal(quary.breadprice.ToString()) < 15 select quary; dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = list2; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public class studentDA { private DataClasses1DataContext _context; public studentDA() { _context = new DataClasses1DataContext(); } //添加数据 public void insert(Student data) { _context.Student.InsertOnSubmit(data); _context.SubmitChanges(); } //修改数据 public void update(Student data) { Student sdata =_context.Student.Single(r => r.Sno == data.Sno); if (sdata != null) { sdata.Sno = data.Sno; sdata.Sname = data.Sname; sdata.Ssex = data.Ssex; sdata.Sbirthday = data.Sbirthday; _context.SubmitChanges(); } } //删除数据 public void delete(Student data) { Student sdata = _context.Student.Single(r=>r.Sno==data.Sno); _context.Student.DeleteOnSubmit(sdata); _context.SubmitChanges(); } ////////根据学生编号删除 public void delete(string sno) { Student data = _context.Student.Single(r => r.Sno == sno); if (data != null) { _context.Student.DeleteOnSubmit(data); _context.SubmitChanges(); } } ///////////查询所有学生数据 public List<Student> select() { return _context.Student.ToList().Count>0? _context.Student.ToList():null; } ///////////////根据学生编号查询 public Student select(string sno) { return _context.Student.Where(r => r.Sno == sno).First(); } /////////////根据学生姓名查询 public List<Student> selectbyname(string sname) { List <Student> list= _context.Student.Where(r => r.Sname.Contains(sname)).ToList(); return list.Count>0? list:null; } } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 往数据库中添加数据 Student data = new Student { Sno=textBox1.Text, Sname=textBox2.Text, Ssex=radioButton1.Checked?"男":"女", Sbirthday=dateTimePicker1.Value, Class=textBox3.Text, }; new studentDA().insert(data); /////////////用linq的方法从数据库中查询数据 private void Form3_Load(object sender, EventArgs e) { List<Student> list = new studentDA().select(); if (list != null) { dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = list; } } //////////用linq的方法从数据库中根据姓名查询 private void button1_Click(object sender, EventArgs e) { List<Student> list = new studentDA().selectbyname(textBox1.Text); if (list != null) { dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = list; } } /////////用linq的方法从数据库中删除数据 private void button2_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { Student data = (Student)dataGridView1.SelectedRows[0].DataBoundItem; new studentDA().delete(data); } dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = new studentDA().select(); } private void button3_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { Form4 f = new Form4((Student)dataGridView1.SelectedRows[0].DataBoundItem); f.Show(); } } private Student sdata;////将选择的信息传到相应的地方构造函数 public Form4(Student data) { InitializeComponent(); sdata = data; } private void Form4_Load(object sender, EventArgs e) { textBox1.Text = sdata.Sno; textBox2.Text = sdata.Sname; textBox3.Text = sdata.Class; if (sdata.Ssex == "男") { radioButton1.Checked = true; } else { radioButton2.Checked = true; } dateTimePicker1.Value = DateTime.Parse(sdata.Sbirthday.ToString()); } ///////////////////////////////////////////////////////////////// ///////将传回的信息修改后传回数据库 private void button1_Click(object sender, EventArgs e) { Student sdata = new Student(); sdata.Sno = textBox1.Text; sdata.Sname = textBox2.Text; sdata.Class = textBox3.Text; sdata.Ssex = radioButton1.Checked ? "男" : "女"; sdata.Sbirthday = dateTimePicker1.Value; new studentDA().update(sdata); this.Close(); }
标签:
原文地址:http://www.cnblogs.com/dawasai/p/4443907.html