In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the example analysis of LinQ to SQL additions, deletions, modifications and queries. I hope you will gain something after reading this article. Let's discuss it together.
Let's take a look at LinQ to SQL, which is a query against a database, which used to be called DLinQ. I often see LinQ. What on earth is that? It is an acronym for Language Integrated Query, a programming method for manipulating objects in a form similar to SQL statements.
Some of the previously mentioned features are also used when using LinQ, such as anonymous types, automation properties, and so on.
LinQ to SQL is an ORM that allows you to control or manipulate relational databases by writing .NET programs to achieve query results. You can use LinQ to retrieve data in the database, or you can update and add data through it. At the same time, LinQ to SQL also supports transactions, attempts, stored procedures, etc., through the LinQ to SQL designer of VS2008 (now using the beta version, the same below), we can easily define some entity classes, we can add a new item to add .dbml file, and drag the data table from the database server window.
We define two entity classes, Artile and Category, and we can also drag stored procedures into the method window. Since our stored procedures and data tables may be named with prefixes or other identifiers, we can look at the property window to change their name properties to names that we can easily identify. Let's open the Designer.cs file to see if there are several classes generated, one of the most important is the class with the suffix DataContext (he uses the current dbml file name plus DataContext as the name of the class), it is the context of this LinQ, we can use it to perform our operations. The above two are foreign key relations, and he also generates a relation class, and there is a collection of Article in the Category class, and there is also an associated attribute in Article, and the type of key value is judged when assigning values.
Let's take a look at some examples of practical application (LinQ to SQL four guardians < SELECT/INSERT/UPDATE/DELETE >):
1. Retrieve data from the database < SELECT >
/ / three overloads (), (string connectionString), (IDbConnection connection)
ItLivesNetDataContext cntx = new ItLivesNetDataContext ()
/ / this can also be initialized directly when initializing the ItLivesNetDataContext instance.
Cntx.Connection = new SqlConnection
(ConfigurationManager.ConnectionStrings [0] .ConnectionString)
Var articles = from an in cntx.Articles
Where a.CategoryID = = 1
Orderby a.PostDate descending
Select a
The above code returns all data of type ID 1 in the database Articles table and sorts it in descending order by release time. Where articles is actually an instance of IEnumerable < T >, and the T of this example is the Article entity class.
two。 Insert new data into the database < INSERT >
/ / insert new type C++ Category category = new Category {CategoryName= "CPLUSPLUS", IsActived=true, ParentID = 1}; / / insert new article, type C++ var article = new Article {Subject = "introduction to C++", Author= "Internet Soul Xiaobing"}; category.Articles.Add (article); cntx.Categories.Add (category); cntx.SubmitChanges ()
3. Update new data < UPDATE >
Category category = cntx.Categories.Single (p = >
P.CategoryName.ToUpper () = = "CSHARP")
Category.IsActived = true
Category.Description = "UPDATE CSHARP!"
Cntx.SubmitChanges ()
4. Delete data from the database < DELETE >
Var delArtiles = from s in cntx.Articles where s.CategoryName = = "CPLUSPLUS" select s; cntx.Articles.RemoveAll (delArtiles); cntx.SubmitChanges (); after reading this article, I believe you have a certain understanding of "sample Analysis of LinQ to SQL additions, deletions, modifications and queries". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
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.