Integration of ADO. NET and LINQ in LINQ to SQL

Source: Internet
Author: User
Tags net command
Now I have a problem. I have some Code It was written in ADO. net. What should I do if I want to add the data to the SQL statements?
See the following code: 1 String Connstring =   @" Data Source =. \ sqlexpress; attachdbfilename = c: \ northwind. MDF;
2 Integrated Security = true; Connect timeout = 30; user instance = true " ;
3 Sqlconnection nwindconn =   New Sqlconnection (connstring); // create a link object
4 Nwindconn. open ();
5
6 Northwnd interop_db =   New Northwnd (nwindconn); // create a datacontext object
7
8 Sqltransaction nwindtxn = Nwindconn. begintransaction (); // create a transaction
9
10 Try
11 {
12 Sqlcommand cmd =   New Sqlcommand (
13 " Update products set quantityperunit = 'single item' where productid = 3 " );
14 Cmd. Connection = Nwindconn;
15 Cmd. Transaction = Nwindtxn;
16 Cmd. executenonquery (); // use ADO. Net to perform the update operation.
17
18 Interop_db.transaction = Nwindtxn; // Add a transaction to datacontext
19
20 Product prod1 = Interop_db.products
21 . First (P => P. productid =   4 );
22 Product prod2 = Interop_db.products
23 . First (P => P. productid =   5 );
24 Prod1.unitsinstock -=   3 ;
25 Prod2.unitsinstock -=   5 ;
26
27 Interop_db.submitchanges (); // perform the operation
28
29 Nwindtxn. Commit (); // submit the transaction
30 }
31 Catch (Exception E)
32 {
33 Console. writeline (E. Message );
34 Console. writeline ( " Error submitting changes all changes rolled back. " );
35 }
36
37 Nwindconn. Close ();

Northwnd is a datacontext object, just like creating the xxxxdatacontext object in LINQ.
Does Code seem so simple to be integrated?
So why can the two things be integrated together? As a part of the ADO. NET Technology series and based on the services provided by ADO. net, you can repeat the connection between the ADO. Net command and a datacontext.

The following code is used to execute an SQL statement in LINQ: 1 Datacontext. executecommand ("Updated products set unitprice = unitprice + 1.00");

Familiar with it. Is it a bit like SQL operations in the enterprise database?
We should all know that LINQ to SQL generates a series of SQL strings, and the last operation is actually an encapsulation of ADO. net.

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.