In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to change the LINQ to SQL database". In the daily operation, I believe many people have doubts about how to change the LINQ to SQL database. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to change the LINQ to SQL database". Next, please follow the editor to study!
LINQ to SQL database verification
The DatabaseExists method is used to attempt to open the database by using a connection in DataContext and return true if successful.
The following code shows whether the Northwind database and the NewCreateDB database exist.
/ / detect whether Northwind database exists if (db.DatabaseExists ()) Console.WriteLine ("Northwind database does not exist"); else Console.WriteLine ("Northwind database does not exist"); / / detect whether NewCreateDB database exists string userTempFolder = Environment.GetEnvironmentVariable ("Temp"); string userMDF = System.IO.Path.Combine (userTempFolder, @ "NewCreateDB.mdf"); NewCreateDB newnewDB = new NewCreateDB (userMDF) If (newDB.DatabaseExists ()) Console.WriteLine ("NewCreateDB database exists"); else Console.WriteLine ("NewCreateDB database does not exist")
LINQ to SQL database changes
The SubmitChanges method calculates the set of modified objects to insert, update, or delete, and executes the appropriate command to implement changes to the LINQ to SQL database.
No matter how many changes the object makes, it is only changing the copy in memory. No changes have been made to the actual data in the database. The changes are not transferred to the server until SubmitChanges is explicitly called on DataContext. When called, DataContext will try to convert the changes we made into the equivalent SQL command. We can also override these operations with our own custom logic, but the commit order is coordinated by a DataContext service called change Handler. The order of events is as follows:
1. When SubmitChanges is called, LINQ to SQL checks the collection of known objects to determine whether the new instance is attached to them. If attached, these new instances are added to the collection of tracked objects.
two。 All objects with pending changes are sorted into a sequence of objects according to their dependencies. If changes to an object depend on other objects, that object will rank after its dependencies.
3. When any actual changes are about to be transferred, LINQ to SQL starts a transaction to encapsulate a series of commands.
4. Changes to the object are converted to SQL commands one by one and then sent to the server.
If the database detects any errors, it will cause the commit process to stop and throw an exception. All changes to the database will be rolled back as if they had not been committed. DataContext still has a complete record of all changes.
The following code shows querying the customer whose CustomerID is ALFKI in the LINQ to SQL database, then changing their company name, * updating and calling the SubmitChanges () method, and updating the data for the second time without calling the SubmitChanges () method.
/ / query Customer cust = db.Customers.First (c = > c.CustomerID = = "ALFKI"); / / update the data and call the SubmitChanges () method cust.CompanyName = "YJingLee's Blog"; db.SubmitChanges (); / / update the data without calling the SubmitChanges () method cust.CompanyName = "http://lyj.cnblogs.com"; so far, the study on" how to change the LINQ to SQL database "is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.