. NET access to the database in two ways (C # language)

Source: Internet
Author: User

A class library that directly uses C # to manipulate the database ADO
Ado. NET uses the connection object to connect to the database, using command or DataAdapter
object to execute the SQL statement and return the result of the execution to DataReader or DataAdapter, and then
Then manipulate the results of the data using the obtained DataReader or DataAdapter objects.

ii. Entity Framework
Entity Framework is an ORM framework for Microsoft. is to support data-oriented software open
The application is sent. We generally work with LINQ and lambda expressions.

An ORM (Object Relational mapping Objects relational mapping) refers to an object-oriented object model and
The transformation of data structures between relational databases. (table entities are converted to each other between tables)

LINQ Queries : When we use LINQ queries, the go to definition is transferred to the Queryable class, which is
said that this class encapsulates the method of all LINQ queries and provides a set of queries to implement Iqueryable<t>
The static method of the data structure.
IQueryable, like IList, is a collection of data that is used to receive a set of iqeurable
(iquerable<t>) does not immediately create persistent data in memory, only traversing it (as
foreach), convert it to a list, and so on, it will load the data into the memory, it can implement "deferred
Line "If the entity (associations) is currently loaded, this associated entity can be
to the access load.

The ilist-inheritance sequence is as follows:
Ilist->icollection->ienumerable
IEnumerable it allows developers to define the implementation of the foreach statement functionality and to support the simplification of non-generic methods
Single Iteration
The same IQueryable also inherits from Ienumerable<t>, a generic interface

The Queryable class provides an extended basic Query method: Where,select
The select provides the following methods:
public static iqueryable<tresult> Select<tsource, tresult> (this
Iqueryable<tsource> source, Expression<func<tsource, tresult>>
selector);
The where provides the following methods:
public static iqueryable<tsource> where<tsource> (this
Iqueryable<tsource> source, Expression<func<tsource, bool>>
predicate);
public static iqueryable<tsource> where<tsource> (this
Iqueryable<tsource> source, Expression<func<tsource, int, bool>>
predicate);

Lambda expression

(1) source. NET designers found that when using anonymous methods, there are still some extra letters or word encoding work

< EM id= "__mcedel" > such as delegate keyword EM id= "__mcedel" > This further simplifies the way anonymous methods are written.

(2) Use
list<int> arr = new List<int> () {1, 2, 3, 4, 5, 6, 7};
Arr. ForEach (New action<int> (delegate (int a) {Console.WriteLine
(a); }));
Arr. ForEach (New action<int> (A = Console.WriteLine (a)));
The code for the anonymous method is as follows:
Delegate (int a) {Console.WriteLine (a);}
The code for using a lambda expression is as follows:
A = Console.WriteLine (a)
Here's an explanation of this lambda expression
<1>
A is an input parameter, and the compiler can automatically infer what type it is,
If you do not have input parameters, you can write this:
() = Console.WriteLine ("DDD")
<2>
= = is the lambda operator
<3>
Console.WriteLine (a) is the statement to execute.
If it is more than one statement, you can use {} to wrap it up.
If you need to return a value, you can write the return statement directly

Linq
1. Query operators
(1) from the source
The designer of. NET defines a series of extension methods in the class library
To facilitate user manipulation of collection objects
These extension methods form the query operators for LINQ
(2) Use
This series of extension methods, such as:
Where,max,select,sum,any,average,all,concat, etc.
are extended for IEnumerable objects.
That is, as long as you implement the IEnumerable interface, you can use these extension methods
Take a look at this code:
list<int> arr = new List<int> () {1, 2, 3, 4, 5, 6, 7};
var result = arr. Where (A = = {return a > 3;}). Sum ();
Console.WriteLine (result);
Console.readkey ();
In this code, two extension methods are used.
(3) Benefits
In the code above
Arr. Where (A = = {return a > 3;}). Sum ();
This sentence can be written in the following code:
(from V in arr where v > 3 select v). Sum ();
And the execution details of the two codes are exactly the same.
As you can see, the second line of code is more semantic and easier to read
The Where in the second line of code is the query operator we want to say.
(4) Standard query operator description
<1> Filtration
Where
Usage: arr. Where (A + = {return a > 3;})
Description: Finds the element in the collection that satisfies the specified condition
OfType
Usage: arr. Oftype<int> ()
Description: Filters the elements in the collection according to the specified type
<2> projection
Select
Usage: arr. Select<int, string> (a = a.tostring ());
Description: A new collection in which each element in the collection is projected. In the example above: The new collection is
A collection of ienumerable<string>
SelectMany
Usage: arr. Selectmany<int, string> (A = = {return new)
List<string> () {"A", A.tostring ()}; });
Description: Projects each element of a sequence into a sequence, eventually putting all the sequences
Merge
<3> There are a lot of query operators, please turn to MSDN, later I will have another article
These operators are all written in the chapter.

2. Query expressions
(1) from the source
As we have mentioned above, using the extension method represented by the query operator to manipulate the collection;
Although already very convenient, but in the readability and the code semantics to consider, still has the insufficiency;
The result is a query expression.
Although this is much like a SQL statement, they are fundamentally different.
(2) Usage
From V in arr where v > 3 Select V
This is a very simple query expression.
(3) Description:
First look at a pseudo-code:
From [type] ID in source
[Join [Type] ID in source on expr equals expr [into
Subgroup]]
[from [Type] ID in source | let id = expr | where condition]
[Ordering,ordering,ordering ...]
Select Expr | Group expr by key
[into ID query]
<1> explanation of the first line:
Type is optional,
ID is an item in the collection,
Source is a collection,
Forced type conversion If the type in the collection differs from the type specified
<2> explanation of the second line:
You can have 0 or more join clauses in a query expression.
The source here can be a whole new collection, not equal to the first sentence in the
Source
Expr can be an expression
[into subgroup] subgroup is an intermediate variable,
It inherits from IGrouping, representing a grouping, which means "one to many"
Much
This variable can be used to get the number of objects contained in this group, and the set of objects
The key
Like what:
From C in DB. Customers
Join O in DB. Orders on C.customerid
equals O.customerid into Orders
Select New
{
C.contactname,
OrderCount = orders. Count ()
};
<3> explanation of the third line:
You can have 1 or more from clauses in a query expression
There can be 0 or more let clauses in a query expression, and a let clause can create a
A temporary variable
Like what:
From U in users
Let number = Int32.Parse (u.username.substring
(u.username.length-1))
Where U.id < 9 && number% 2 = = 0
Select U
There can be 0 or more WHERE clauses in a query expression, where clauses can be
To specify the query criteria
<4> Explanation of Line fourth:
A query expression can have 0 or more sort methods
Each sorting method is separated by commas
<5> explanation of line fifth:
A query expression must end with Select or group by
Select followed by what to retrieve
Group BY is a group of content that is retrieved
Like what:
from P in Db. Products
Group p by P.categoryid into G
Select New {g.key, numproducts = G.count ()};
<6> Explanation of line sixth:
The last into clause acts as a
Data source that takes the result of the preceding statement as a subsequent statement
Like what:
from P in Db. Employees
Select New
{
LastName = P.lastname,
TitleOfCourtesy = P.titleofcourtesy
} into Employeeslist
Employeeslist.titleofcourtesy Ascending
Select Employeeslist;

. NET access to the database in two ways (C # language)

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.