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.