Linq 数据库操作(增删改查)

  Linq数据库增删改查 

  Linq是一种查询语言,集成包含在formwork中,包含在C#语言中,它的作用是降低查询的门槛,提高开发效率,是我们必须掌握的技术之一,下面是我自己对linq数据库操作的方法,与大家分享一下。

 

  1、首先是添加:我自己的添加理念是,先查看一下添加的主键是否存在,以免重复添加,然后为添加的新行赋值,最后添加刷新数据库实现添加。

  如:

    public static bool Add(string s1, string s2, string s3, string s4)        //s1为添加的编号是主键,s2,s3,s4分别是要添加的姓名、地址、电话

      {           

         // new一个linq类对象

         DataClasses1DataContext dc = new DataClasses1DataContext();            

        //定义一个bool类型值,初始化为false            

         bool b = false;            

        //linq语句            

        var re = from list1 in dc.list                     

            where list1.LId == s1                     

            select list1;            

         //查看学号是否已存在,修改bool值            

         foreach (var i in re)            

        {                

           string s = i.LId;

                     b = true;                           

        }            

        if (b == false)            

         {                

          //为添加行赋值                

           list l = new list();                

           l.LId = s1;                

           l.LName = s2;                

           l.LAddress = s3;                

           l.LPhone = s4;

                      dc.list.InsertOnSubmit(l);  //实现添加                

            return b;            

        }            

         return b;        

    }

 

  2、然后是修改,修改是先查到要修改的主键,然后为修改项赋值,最后刷新数据库实现修改

  如:

    public static bool Update(string s1, string s2, string s3, string s4)
        {
            //定义一个bool类型值
            bool b = false;
            try
            {
                DataClasses1DataContext dc = new DataClasses1DataContext();
                //linq执行语句
                var re = from l in dc.list
                         where l.LId == s1
                         select l;
                //实现修改
                foreach (list i in re)
                {
                    i.LName = s2;
                    i.LAddress = s3;
                    i.LPhone = s4;
                    dc.SubmitChanges(); //实现数据库更新
                }
            }
            catch
            {
                b = true;
            }
            return b;
        }

  3、然后是删除,删除比较简单,找到要删除的主键,执行删除方法,刷新数据库就行了

  如:

    
        public static bool Delete(string s)
        {
            //定义一个bool类型值
            bool b = false;
            try
            {
                DataClasses1DataContext dc = new DataClasses1DataContext();
                //linq语句
                var re = from l in dc.list
                         where l.LId == s
                         select l;
                //实现删除
                dc.list.DeleteAllOnSubmit(re);
                //刷新数据库
                dc.SubmitChanges();
            }
            catch
            {
                b = true;
            }
            return b;
        }

  4、最后是查询了,linq查询语句、绑定就ok了

    如:  

    DataClasses1DataContext dc = new DataClasses1DataContext();
            //linq语句
            var re = from l in dc.list
                     select l;
            dataGridView1.DataSource = re;

posted @ 2013-07-04 21:21  岳帅超  阅读(1375)  评论(0编辑  收藏  举报