A simple example LinqtoSql classes learning C #

Reprinted to  https://www.cnblogs.com/zouzf/archive/2012/02/28/2371740.html 

LinqtoSql the details of accessing the class, package up operation of the database, the database connecting operation becomes quite easy. Here is a simple example. 

The first step: Add LinqtoSql class

1. Create a console application project, download a NrothWind database solutions put inside. Then right-click console project select Add, select the class

Select the LINQ to SQL classes, the name was changed to NorthWindDB, because one will be connected to NorthWind database.

2. At this point, the contents of the following solutions

3, NorthWindDB.dbml double-click the file, and then click the left mouse edit box, press F4, the right will pop up NorthWindDB.dbml file attribute information related

NorthWindDBDataContext is compiler automatically generates a class to inherit DataContext class is the class encapsulates the details of the operation of the database. Context namespace (that is, NorthWindDBDataContext class where the namespace), the default is empty, we add to it NorthWindDBCont. Entity namespace (the compiler will link to our database which we use tables are mapped to a corresponding class (column corresponds to the class attribute table), those classes corresponding table mapping out to belong to this namespace) the default is empty, fill NorthWindDBEnts.

 

Step Two: Visualization connect to the database

1. Select the View toolbar, choose Server Explorer, right-click on a data connection, select the Add Link

2. Add Connection dialog box pops up, click Change

3. Change the Data Source pop-up dialog box, select Microsoft SQL Server database files

4, Point Browser

5. Select the database file just downloaded NorthWind.MDF

6、点左下角的测试,如不成功,重新操作 第二步。如果成功,打开服务器资源管理器,可以看到NorthWind数据库已经加载成功。双击NorthWindDB.dbml文件,把里面的表 Customers 、 Orders拖过去

这个时候,编译器就会自动在 NorthWindDBEnts命名空间里创建两个类来对应着这两个表。

打开NorthWindDB.designer.cs文件,NorthWindDBEnts命名空间下的代码如下:

 

第三步:主代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建NorthWindDBCont.NorthWindDBDataContext的实例
            NorthWindDBCont.NorthWindDBDataContext ctx = new NorthWindDBCont.NorthWindDBDataContext();

            //查询住在London的顾客
            var q = from x in ctx.Customers
                    where x.City == "London"
                    select new { x.ContactName, x.City};

            if (q.Count() != 0)
            {
                foreach (var item in q)
                {
                    Console.WriteLine(item);
                }              
            }
            else
            {
                Console.WriteLine("nobody in London");
            }
            Console.WriteLine();

            //创建NorthWindDBEnts.Customers 的实例
            NorthWindDBEnts.Customers myCustomer = new NorthWindDBEnts.Customers();

            //对表Customers进行查询,把查询得到的住在London的顾客
//并把查得的第一个顾客返回赋给NorthWindDBEnts.Customers类 的实例
            myCustomer = ctx.Customers.First(a => a.City == "London");
            Console.WriteLine(myCustomer.ContactName);
            
            Console.ReadKey();
        }
    }
}

 

输出如下:

Guess you like

Origin www.cnblogs.com/jshchg/p/12366629.html