Getting Started Series C # 3.0 (c)

From the beginning of this section, the author and everyone will begin to experience the dlinq together. Earlier we prepared a database, but also the relationship between the database to do a preliminary understanding. Once you have a database, data and objects is what kind of relationship? Dlinq from the design point of view, it is mainly to solve the data! = Objects of problems arising. Well, now, with the dlinq you can have a one to one relationship between the data and objects. We not only can generate this code database mapping, you can also generate a database based on the mapping code. Simply put, the database and the mapping code to achieve the mutual conversion. linq preview provides a good tool that can help us achieve alluding to the code from the database. It is sqlmetal. beta2 of sqlmetal in C: \ Program Files Microsoft SDKs \ \ v6.0A under \ Windows \ bin directory or C: \ WINDOWS \ Microsoft.NET \ Framework \ v3.5 directory

open cmd, run sqlmetal program. The following prompt will appear.
Run the following command. SQLMetal / Server: myserver / Database: northwind / namespace: the NWIND /code:nwind.cs
/ Language: csharp
you can generate a file based on nwind.cs northwind database. You will find it in the bin directory ^ _ ^ linq priview of. Meaning of the parameters here are also very clear, we do not repeat them in the multi.

Here, I would like to briefly introduce this mapping file nwind.cs. Because this program is automatically generated, we do best not to change in later advanced, I will elaborate on the meaning of all this document code, and implement inheritance.
We first look at the definition of Northwind class.
    public partial class Northwind: DataContext {. . . .
First partial keyword in C # 2.0 is emerging, this article is not to explain C # 2.0, the relevant knowledge refer to the relevant literature. Northwind's name is based on the name of your database definition. We found that it must inherit from DataContext class, in order to gain support of dlinq. Further down
        public Table <Order> Orders;

        public Table<Product> Products;

        the Table public <the OrderDetail> the OrderDetails;
the Table dlinq class is defined, where he used a range of concepts, similar to the template in C ++. However, C # and C ++ there are differences, C ++ generic template similar substitutes, just replace it with the actual type compile time, so any type can be, but C # generics are implemented on a virtual machine level, so when compiling type checking (cited). Order, Product OrderDetail and the like, according to the program are sqlmetal corresponding database table, automatically generated classes. That is class and the table is one to one. And this is precisely the realization of the object and data and so on. Let's go look at the definition Order it. I posted here only partially. Only a few brief points, I will explain in more detail the contents of the media into the stage, including inheritance. 

[Table(Name  =   " Orders " )]
    
public  partial  class  Order : System.Data.DLinq.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged 
ExpandedBlockStart.gifContractedBlock.gif
{
        
private int _OrderID;
        
private string _CustomerID;
        
private System.Nullable<int> _EmployeeID;

…………
ExpandedSubBlockStart.gifContractedSubBlock.gif        
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)]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public int OrderID {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return this._OrderID;
            }

        }


}




Because the mapping files are generated automatically, you do not need to manually manufacture our own, we save a lot of labor. Here we can see the Order class must be inherited from both interfaces, but also the method defined in the interface to achieve. It also defines a number of private variables, and these seem to data table fields does not matter. In fact, it describes the way to go in the Property. Property dlinq will correspond to a field definition table for each data, then, will be added thereto attribute , such as
[Column (Storage = "_OrderID" , DBType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDBGenerated = true )]
is used to describe the corresponding data table that field Property (Storage), what type (DBType), whether the main key (IsPrimaryKey), whether to automatically increase (IsDbGenerated) and the like. Here, we were being asked not to modify this file, so you are more familiar with it after, you can inherit with its own definitions, such as there is a good product under a variety of product yet. I'll talk about Shinsuke in.

With the mapping file, you are not eager, eager to create your own linq project of it? After you install the linq preview, select
file-> new-> Project. You will find more than usual options. as the picture shows.

After selecting linq preview, select a linq console application, add name, let us immediately begin linq journey. After creating the project, you will have your own front nwind.cs added to the project file, 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
ExpandedBlockStart.gifContractedBlock.gif
{
    
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Northwind db 
= new Northwind("Your Connection String");

        var q 
= from c in db.Customers
                 select c;
        
foreach (var c in q)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(c.ContactName);
        }


    }



}




Run, ha ha, your first linq preview of the project up and running, and happy cutting. Oh, good start from the next chapter, the author will focus on to explain to you dlinq syntax. Starting with the next chapter, this series will be renamed entry series, Oh, I had no books to read. It will be released simultaneously in the home Digest, attention dlinq friends to receive attention Oh.

 

 

TrackBack: http://www.cnblogs.com/126/archive/2006/09/06/492332.html

Reproduced in: https: //www.cnblogs.com/hdjjun/archive/2008/11/05/1327187.html

Guess you like

Origin blog.csdn.net/weixin_33938733/article/details/94497522