C #3.0 LINQ ing of the LINQ database table

Source: Internet
Author: User
C #3.0 LINQ ing of the LINQ database table [add this page to favorites] [print] Author: ITPUB Forum2008-01-17 Content Navigation:

Overview

Page 1: Overview

Linq to SQL is a database application of LINQ technology. Database technology from OLEDB, odbc to ado, to ado. net to the present of the linq to SQL, making it easier for programmers TO operate databases. The purpose of LINQ is to make queries no longer exist. Of course, queries to databases should be included. Not only can the database be queried, but also CUID (Create, Update, Insert, Delete) can be implemented and very convenient.

The following describes query and addition, deletion, and modification.

To query the data in a database table, you must first establish a ing to the database table. Just like to use ADO. NET, you need to first store the data in the database to DataSet and take a look at the code. /* Note that the [Table] feature corresponds to the Table named "Category" in the database. The [Column] feature corresponds to the columns in the database. * /// I don't want to explain it in detail here. I will introduce these topics in the future when I write my own special topics. [Table (Name = "Category")] publicclass Category... {[Column (IsPrimaryKey = true)] publicstring CategoryId; [Column] publicstring Name; [Column] publicstring Descn ;}

The above is a ing of the data table. The database used is the database Northwind that comes with SQL SERVER2000. In fact, people who have written C # should be able to understand the above Code. After the table ing is established, you can perform corresponding operations on the table.

Note that we use manual data table ing here. In fact, MS provides us with the corresponding automatic tool SqlMetal. You can use this tool to automatically generate code for database table ing, which is highly efficient, you can change the generated code if you have any changes.

The following query is available: DataContext db = new DataContext ("Server = localhost; Database = northwind; Trust_connection = true "); // The connection string here can be changed according to your own configuration. Table <Category> Categorys = db. getTable <Category> (); var result = from c in Categorys select c; // You Can See That DataContext is equivalent to ADO. NET Connection, but it provides more powerful functions // The result is equivalent to IEnumerable <Category> if you do not know why, for more information, see the previously written Lambda expression. // The content in result foreach (var c in result) Console can be output below. writeLine ( "CategoryId = {0}, Name = {1}, Descn = {2}", c. categoryId, c. name, c. descn);/** // * Here is a brief description of the smart sensing of LINQ. If you are using a LINQ project created by the VS2005 + LINQ plug-in, there is no smart awareness of linq to SQL in. That is to say, when you write c in the output, there will be no content, but you can write the corresponding fields according to the self-built ing, and there is no problem in compiling and running. If you use VS2008, that is, the LINQ project code "Orcas", then intelligent sensing is supported. Very important */

The above is the query of the database. we can write a very complex query. In fact, internally, LINQ will convert your written LINQ statements into SQL statements and send them to the database for execution. Then return the corresponding result. If you want to see the converted SQL statement, you can add db. Log = Console. Out after the connection is established. In this way, the program will automatically output SQL statements and query results. Here is a brief introduction to how to query. We know that there is a relationship between the table and the table. We will explain these complicated things in detail later.

Next let's take a look at 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 to the database. submitChanges (); // note that SubmiChanges () has changed the Object layer to the data layer. That is to say, if you do not submit the changes, the changes will not be stored in the database. 1

Related Article

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.