C #3.0 getting started series (III)

Source: Internet
Author: User

Starting from this section, I will start to experience dlinq with you. We have prepared a database and made a preliminary understanding of the relationship between databases. What is the relationship between data and objects with the database? From the design of dlinq, it mainly aims to solve the problem of data! = Problems with objects. Now, there is a one-to-one relationship between data and objects after dlinq. We can generate this shadow code based on the database, or generate a database based on the shadow code. Simply put, the database and the shadow code implement mutual conversion. LINQ preview provides a good tool for us to learn from the database to the code. It is sqlmetal. Sqlmetal of beta2 is in the c: \ Program Files \ microsoft sdks \ windows \ v6.0a \ bin directory, or the c: \ windows \ Microsoft. NET \ framework \ v3.5 directory.

Open CMD and run the sqlmetal program. The following prompt is displayed.
Run the following command. Sqlmetal/Server: myserver/Database: northwind/namespace: nwind/code: nwind. CS
/Language: CSHARP
You can generate an nwind. CS File Based on the northwind database. You will find it in the bin directory of LINQ priview ^_^. The parameter meanings here are also very clear, and I will not go into details here.

Here, I 'd like to brief you on the nwind. CS. because this is automatically generated by the program, we 'd better not change it for the moment. In the next step, I will elaborate on the meaning of the Code in this file and the implementation of inheritance.
Let's first look at the definition of the northwind class.
Public partial class northwind: datacontext {....
First, the keyword partial appears in C #2.0. This article does not explain C #2.0. For more information, see relevant literature. The name of northwind is defined based on the name of your database. We found that it must be inherited from the datacontext class to obtain dlinq support. Let's look down.
Public table <order> orders;

Public table <product> products;

Public table <orderdetail> orderdetails;
The table class is defined in dlinq. Here he uses an exemplary concept, similar to a template in C ++. However, C # And C ++ are different. The C ++ generic template is similar to a replacement, but it is replaced by the actual type during compilation. Therefore, it can be of any type, however, the generic type of C # is implemented at the virtual machine level, so the type check (reference) is performed during compilation ). Order, product, and orderdetail are all classes automatically generated by the sqlmetal program based on the corresponding tables in the database. That is, the class and table correspond one by one. This achieves the equality between objects and data. Let's take a look at the definition of order. I will only post some of them here. I will briefly introduce a few points. For more details, I will explain them at the introduction stage, including inheritance.

[Table (name = "orders")]
Public partial class order: system. Data. dlinq. inotifypropertychanging, system. componentmodel. inotifypropertychanged
{
Private int _ orderid;
Private string _ customerid;
Private system. nullable <int> _ employeeid;

............
Public Order (){
This. _ orderid = default (INT );
This. _ orderdetails = new entityset <orderdetail> (new notification <orderdetail> (this. attach_orderdetails), new notification <orderdetail> (this. detach_orderdetails ));
This. _ customer = default (entityref <customer> );
This. _ Employee = default (entityref <employee> );
This. _ shipper = default (entityref <shipper> );
}

[Column (storage = "_ orderid", dbtype = "int not null identity", isprimarykey = true, isdbgenerated = true)]
Public int orderid {
Get {
Return this. _ orderid;
}
}

}


Because the photo and video files are generated automatically, we do not need to manually create them, which saves us a lot of labor. Here, we can see that the Order class must inherit from two interfaces and implement the methods defined in interfaces. It also defines some private variables that seem to have nothing to do with fields in the data table. Actually, it is described in property. Dlinq defines a corresponding property for each data table field, and then adds attribute to it, as shown in figure
[Column (storage = "_ orderid", dbtype = "int not null identity", isprimarykey = true, isdbgenerated = true)]
It is used to describe the field (storage) of the data table corresponding to the property, its type (dbtype), its primary key (isprimarykey), and its automatic addition (isdbgenerated. Here, we do not want to modify this file for the time being. After you are familiar with it, you can use it to define your own inheritance. For example, there are many products under the product. I will introduce it in the introduction.

After you have installed the image file, are you eager to create your own LINQ project? After you install LINQ preview, select
File-> New-> project. You will find that there are more options than usual ..

After selecting the LINQ preview, select a LINQ console application and add a name to it. Let's get started with the LINQ journey. After creating the project, add the nwind. CS file generated before you to the project, and type the following code in program. CS. Using system;
Using system. Collections. Generic;
Using system. text;
Using system. query;
Using system. xml. xlinq;
Using system. Data. dlinq;
Using system. Data;
Using system. Data. sqlclient;
Using nwind;

Class Program
{
Static void main (string [] ARGs)
{
Northwind DB = new northwind ("your connection string ");

VaR q = from C in db. MERs
Select C;
Foreach (var c in Q)
{
Console. writeline (C. contactname );
}

}


}


Run. Haha, your first LINQ preview project is running. Well, from the next chapter, I will focus on explaining the dlinq syntax. Starting from the next chapter, this series will be renamed as the entry series. Haha, There is no book to read. At the same time, it will be published in the home page essence area, so that users who are interested in dlinq will pay attention to it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.