Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use LINQ query Technology

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use LINQ query technology". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

LINQ TO SQL is the application of LINQ technology in database. Database technology from OLEDB,ODBC to ADO, to ADO.NET to now LINQ TO SQL, makes it easier and easier for programmers to operate the database. The purpose of LINQ is to make queries ubiquitous, including, of course, queries to databases. LINQ technology can not only query the database, but also CUID (Create,Update,Insert,Delete) can be realized, and it is very convenient.

The following is an introduction to the query, addition, deletion and modification one by one

If you want to query the data of the tables in the database, you must first establish the mapping to the database tables, just as if you want to use ADO.NET, you need to save the data in the database to DataSet. Take a look at the code.

[Table (Name = "Category")] public class Category... {[Column (IsPrimaryKey = true)] public string CategoryId; [Column] public string Name; [Column] public string Descn;}

The above is a mapping to the data table. The database used is the database Northwind that comes with SQL SERVER2000. In fact, people who have written C# should also be able to understand the above code. Once the mapping to the table is established, you can manipulate it accordingly.

Note that here we use handwritten data table mapping, in fact, MS provides us with the corresponding automation tool SqlMetal, we can use this tool to automatically generate database table mapping code, more efficient, if you need to change anything, you can also change the resulting code.

Now you can query it.

DataContext db = new DataContext ("Server=localhost

Database=northwind;Trust_connection=true ")

/ / the connection string here can be changed according to its own configuration.

Table Categorys = db.GetTable ()

Var result =

From c in Categorys

Select c

/ / you can see that DataContext is equivalent to Connection in ADO.NET, but it provides more powerful features.

/ / where result is equivalent to IEnumerable. If you don't know why you launched this,

You can refer to the Lambda expression that I originally wrote.

/ / you can output the contents of result below

Foreach (var c in result)

Console.WriteLine ("CategoryId= {0}, Name= {1}, Descn= {2}"

C. CategoryIdrec. NameMagnec. Descn)

These are the queries to the database. We can write very complex queries. In fact, internally, LINQ technology will convert the LINQ statements you write into SQL statements and send them to the database for execution. And then return the corresponding results. If you want to see the converted SQL statement, you can add this sentence after the connection is established: db.Log=Console.Out. In this way, the program will automatically output SQL statements and query results. Here is just a brief introduction to how to query, we know that there is a relationship between tables, these complex things we will explain in detail later.

Let's see how to change it.

/ / change

String id = "DOGS"

Var cat = db.Categories.Single (c = > c. CategoryId = = id)

Cat. Name = "New Dogs"

/ / add

Product p = new Product. {. .}

Cat.Products.Add (p)

/ / Delete

String id = "DOGS"

Var cat = db.Categories.Single (c = > c. CategoryId = = id)

Db.Categories.Remove (cat)

/ / submit changes

Db.SubmitChanges ()

/ / Note that SubmiChanges () completes the change from the object tier to the data tier.

/ / that is, no commit is made, and the results of the changes are not stored in the database.

This is the end of the content of "how to use LINQ query Technology". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report