(LINQ 学习系列)(5)Linq教程实例: 单表操作之增 删 改 查询

新增操作
private void toolStripBtnAdd_Click(object sender, EventArgs e)
        {
            //新建立一个类,然后属性赋值
            student mystudent = new student();
            mystudent.StudentName = "MeetWeb";
            mystudent.Sex = "Man";
            mystudent.Old = 17;
           DataClasses1DataContext db = new DataClasses1DataContext(sqlconStr);
            db.student.InsertOnSubmit (mystudent);
            db.SubmitChanges();
            DataLoad();
            //this.dataGridView1.Refresh();
        }

删除操作


private void toolStripBtnDel_Click(object sender, EventArgs e)
        {
            //Delete Action
           DataClasses1DataContext db = new DataClasses1DataContext(sqlconStr);
           //Need to Delete the ID is 6
            student mystudent = db.GetTable<student>().First<student>(p => p.ID == 4);
            db.student.DeleteOnSubmit(mystudent);
        }

更新操作

   

    private void toolStripBtnSave_Click(object sender, EventArgs e)
        {
            //Modify Action
            DataClasses1DataContext db = new DataClasses1DataContext(sqlconStr);
            //Need to update the ID is 6
            student mystudent = db.GetTable<student>().First<student>(p => p.ID == 5);
            mystudent.Old = 18;
            mystudent.Sex = "woman";
            db.SubmitChanges();
        }


多条件的查询

private List<student> Search(string id, string studentname)
        {
            DataClasses1DataContext db = new DataClasses1DataContext(sqlconStr);
            List<student> mystudent = db.GetTable<student>().First<student>(p => p.ID == 4&&p.
studentname=studentname).ToList();
            return mystudent  ;
        }

 

 

posted @ 2012-03-24 10:01  meetweb  阅读(945)  评论(0编辑  收藏  举报