linq to sql简单使用

2018-06-23 22:02:05来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

1.新建一个winform项目。

2.添加一个Linq to Sql 类,命名为Northwind

3.打开服务器资源管理器,将表拖动到linq to sql 类,实体类就由Vs生成了

4.实例化DataContext类

NorthwindDataContext dc = new NorthwindDataContext();

5.下面就可以使用Linq to Sql啦。

a.查询Employees所有数据

var ee =
from employee in dc.Employees
select employee;

b.加个Where条件,查询Employee表所有City=London

            var e2 =
                from employee in dc.Employees
                where employee.City == "London"
                select employee;

 

c.查询EmployeeID<5的数据

            var e3 =
                from employee in dc.Employees
                where employee.EmployeeID < 5
                select employee;

 

d.查询雇佣日期在1993年1月1日以前的人员。

            var e4 =
                from employee in dc.Employees
                where employee.HireDate < new DateTime(1993, 1, 1)
                select employee;

 

e.多个查询条件

            var e5 =
                from employee in dc.Employees
                where employee.HireDate < new DateTime(1993, 1, 1)
                where employee.EmployeeID < 3
                select employee;

 

f.like语句

           var e6 =
                from employee in dc.Employees
                where SqlMethods.Like(employee.FirstName, "%a%")
                //where employee.FirstName.Contains("a")    //等价于 like '%a%'
                //where employee.FirstName.StartsWith("a")  //等价于 like 'a%'
                //where employee.FirstName.EndsWith("a")    //等价于 like '%a'
                select employee;

 

g.查询指定的列

           var e7 =
                (from employee in dc.Employees
                 //select new { employee.EmployeeID, employee.FirstName };
                 select new
                 {
                     员工编号 = employee.EmployeeID,
                     员工姓名 = employee.FirstName,
                     尊称 = employee.TitleOfCourtesy
                 });

 

h.子查询

            var e8 =
                from employee in dc.Employees
                where employee.Orders.Count > 100
                select employee;

 

i.内连接

            /**
             * 查询位于伦敦的客户的订单
             * SELECT *
             * FROM [dbo].[Orders]
             * INNER JOIN [dbo].[Customers] ON [Customers].[CustomerID] = [Orders].[CustomerID]
             * WHERE [Customers].[City] = 'London'
             */
            var e9 =
                from cus in dc.Customers
                join ord in dc.Orders on cus.CustomerID equals ord.CustomerID
                where cus.City == "London"
                select ord;

 

j.增加

            Employees m = new Employees();
            m.LastName = "闪硕";
            m.FirstName = "";
            m.Title = "闪硕标题";
            dc.Employees.InsertOnSubmit(m);
            dc.SubmitChanges();

 

k.修改

            var e10 = dc.Employees.FirstOrDefault(employee => employee.LastName == "闪硕");
            e10.City = "石家庄";
            dc.SubmitChanges();

 

l.删除

            var e10 = dc.Employees.FirstOrDefault(employee => employee.LastName == "闪硕");
            dc.Employees.DeleteOnSubmit(e10);
            dc.SubmitChanges();

 

练习代码使用Vs2012,微软northwnd示例数据库

代码下载:http://pan.baidu.com/s/1pJyh0Mn  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:如何使用vs将现有的项目或者文件夹(尤其是多层目录的)添加到项

下一篇:MVC3学习随记一