[Import] Linq learning notes (2.3) -- Advanced DLinq operations

Source: Internet
Author: User

Previously we learned some basic knowledge about using Dlinq to operate databases from the object-oriented perspective. Today I learned how to directly execute SQL commands to query and execute stored procedures in Dlinq, some knowledge about transaction processing. Below are some demos for my contact.
Before learning this course, I would like to share with you two knowledge about Dlinq:
1: use tools to generate objects.
During the previous study, you may feel that the most troublesome thing is to write ing entities based on the database. vs provides a dedicated tool (C: \ Program Files \ Microsoft Visual Studio 9.0 \ SDK \ v3.5 \ Bin \ SqlMetal.exe) to automatically generate objects,
SqlMetal/server:. \ SQLExpress/database: Northwind/pluralize/namespace: Arcadia/code: d: \ Northwind. cs
You can find Northwind. cs in the d: Directory and copy it to your project. The following code is used in this file.

2: displays compiled SQL statements of the Linq statement.
Method 1: Use the Log attribute of DataContext. Db. Log = Response. Output;
Method 2: Use GetQueryText/GetChangeText. GetQueryText is used for query, and GetChangeText is used for update or deletion.

Var products = db. Products. Where (p => p. OrderDetails. Count> 50 );
Response. Write (db. GetQueryText (products ));

ExecuteQuery

Northwind db = new Northwind ("data source =. \ SQLEXPRESS; Integrated Security = SSPI ;");
Var products = db. ExecuteQuery <Product> ("select * from Products where ProductID <10 ");
Products. ToList (). ForEach (p => Response. Write (p. ProductName + "<br> "));

ExecuteCommand

Northwind db = new Northwind ("data source =. \ SQLEXPRESS; Integrated Security = SSPI ;");
Db. ExecuteCommand ("update [Products] set [ProductName] = {0} where [ProductID] = {1}", "Young's book", 1 );

ExecuteCommand is used to perform operations such as update and delete operations. ExecuteQuery is used for query operations. Therefore, different statements must be used when executing stored procedures.

Northwind db = new Northwind ("data source =. \ SQLEXPRESS; Integrated Security = SSPI ;");
Var orders = db. ExecuteQuery <Order> ("exec CustOrdersOrders @ CustomerID = {0}", "ALFKI ");
Orders. ToList (). ForEach (o => Response. Write (o. OrderID + "<br> "));

Transactions

Northwind db = new Northwind ("data source =. \ SQLEXPRESS; Integrated Security = SSPI ;");

If (db. Connection! = Null) db. Connection. Open ();
Db. Transaction = db. Connection. BeginTransaction ();
IEnumerator <OrderDetail> orders = db. OrderDetails. Where (o => o. ProductID = 1). GetEnumerator ();
While (orders. MoveNext ())
{
OrderDetail od = orders. Current;
Od. UnitPrice = 200;
}

Var prod = db. Products. Single (p => p. ProductID = 1 );
Db. Products. Remove (prod );
Try {
Db. SubmitChanges ();
Db. Transaction. Commit ();
}
Catch {
Db. Transaction. Rollback ();
Throw;
}
Finally {
Db. Transaction = null;
}

The official document introduces how to use Transaction of Dinq, but I don't understand it. Even if Transaction is not used, SubmitChanges () itself has a function similar to Transaction, if one operation fails in a series of operations, the operation will not succeed. analyze the difference between using Transaction and directly SubmitChanges.
The above are complex database operation scenarios used to make up for the poor processing of Dlinq objects. The above example is very simple, in order to facilitate understanding of its usage.

Young. J comment [group] [blog] [Flash Memory]

Source: http://www.cnblogs.com/young18/archive/2007/06/17/786651.html

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.