Home Database Mysql Tutorial LINQ to Sql系列一 增,删,改

LINQ to Sql系列一 增,删,改

Jun 07, 2016 pm 05:44 PM
linq series

LINQ to Sql系列一 增,删,改 首先,我们来了解一下LINQ to sql 是什么,我想从以下几点来做说明。 1,LINQ是从.net framework 3.0开始引入的的语言特性,主要是用来对集合数据进行操作的。 2,LINQ to sql是LINQ在数据库数据访问方面的一个ORM框架。 了解

LINQ to Sql系列一 增,删,改

首先,我们来了解一下LINQ to sql 是什么,我想从以下几点来做说明。

1,LINQ是从.net framework 3.0开始引入的的语言特性,主要是用来对集合数据进行操作的。
2,LINQ to sql是LINQ在数据库数据访问方面的一个ORM框架。

了解完概念之后,我们就开始地学习LINQ to sql了,不过在正式利用LINQ to sql去进行数据的增,删,改,查之前,我们还有一些准备工作要做。

准备工作

1,创建数据库

创建数据库名为LinqtoSqlDemoDb,一共有四张表分别是Student学生表,Class班级表,Course课程表和StudentCourse学生课程表,如图:

l2s01

注意表与表之间的外键关系。

2,创建LINQ to classes文件

打开VS2010,创建一个类库工程命名为LinqtoSqlDemo.DAL,在工程上右键单击,选择“add--->add item”,服务器空间,在文件类型中选择“LINQ to classes”,命名为L2SDB.dbml,如图所示:

l2s02

3,根据数据库表自动生成代码

创建好LINQ to classes文件后,选择菜单“view--->server explorer”打开server explorer窗口,增加一个数据库连接。如图一

l2s03

展开tables开点,应该就能看到刚才创建的四张表,将它们选中然后拖到设计区,就会得到如图所示的样子。

l2s04

在设计窗口按F4键打开DataContext的属性窗口,我们需要给数据库上下文设定命名空间,如图。

l2s05

按ctrl+f5保存,香港服务器,这样我们的LINQ to sql 的ORM就创建好了。下面我们就可以开始数据的增,删,改的操作了。

代码如下:

Add() { ClassInfo ci = }; Console.WriteLine(); using (L2SDBDataContext db = new L2SDBDataContext()) { TClass tc = new TClass(); ClassValueCopier.Copy(tc, ci); db.TClasses.InsertOnSubmit(tc); db.SubmitChanges(); } Console.WriteLine(); }

代码如下:

Delete(int id = 3) { Console.WriteLine(); using (L2SDBDataContext db = new L2SDBDataContext()) { //get the class by id TClass tc = db.TClasses.SingleOrDefault(p => p.ClassID == id); db.TClasses.DeleteOnSubmit(tc); db.SubmitChanges(); } Console.WriteLine(); }

代码如下:

Edit(int id = 3) { Console.WriteLine(); using (L2SDBDataContext db = new L2SDBDataContext()) { //get the class by id TClass tc = db.TClasses.SingleOrDefault(p => p.ClassID == id); tc.ClassName = ; db.SubmitChanges(); } Console.WriteLine(); }

总结:

1,首先我们必须得到DataContext对象,这个对象相当于一个数据库的GateWay,美国空间,所有的操作都是通过它进行的。

2,LINQ to sql使用了“Unit of work”模式,所以,对数据库的操作不会立即提交到数据库,而是需要显式调用DataContext对象的SubmitChanges方法,所有改动才会被提交到数据库中。

posted on

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Xiaomi 15 series full codenames revealed: Dada, Haotian, Xuanyuan Xiaomi 15 series full codenames revealed: Dada, Haotian, Xuanyuan Aug 22, 2024 pm 06:47 PM

The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers

The best time to buy Huawei Mate 60 series, new AI elimination + image upgrade, and enjoy autumn promotions The best time to buy Huawei Mate 60 series, new AI elimination + image upgrade, and enjoy autumn promotions Aug 29, 2024 pm 03:33 PM

Since the Huawei Mate60 series went on sale last year, I personally have been using the Mate60Pro as my main phone. In nearly a year, Huawei Mate60Pro has undergone multiple OTA upgrades, and the overall experience has been significantly improved, giving people a feeling of being constantly new. For example, recently, the Huawei Mate60 series has once again received a major upgrade in imaging capabilities. The first is the new AI elimination function, which can intelligently eliminate passers-by and debris and automatically fill in the blank areas; secondly, the color accuracy and telephoto clarity of the main camera have been significantly upgraded. Considering that it is the back-to-school season, Huawei Mate60 series has also launched an autumn promotion: you can enjoy a discount of up to 800 yuan when purchasing the phone, and the starting price is as low as 4,999 yuan. Commonly used and often new products with great value

How to choose between iPhone 15 and iPhone 15 Pro? Nine major differences at once How to choose between iPhone 15 and iPhone 15 Pro? Nine major differences at once Sep 14, 2023 am 08:01 AM

iPhone15 and iPhone15Pro were officially released today. However, as high-end models, the Pro series not only has a higher price, but also has many exclusive features. Consumers must recognize the differences before buying, so as not to discover some problems after buying iPhone15. The function is only available in the Pro series. Although the monitors are equipped with the same display panel, the ProMotion automatic adaptive update frequency technology and the always-on display function are still exclusive to the Pro series. The rest of the iPhone 15 and iPhone 15 Pro series are the same in terms of resolution, contrast, peak brightness, etc. Action button The action button is currently an exclusive design for the iPhone 15 Pro series, allowing users to personalize it.

What series are there in the Hall of Fame? What series are there in the Hall of Fame? Feb 04, 2024 am 09:18 AM

Many users who want to buy memory modules want to know what series of memory modules the GALAX Hall of Fame brand has. In fact, this brand currently has three series, namely HOFEXTREME limited edition, HOFEXTREME, and HOFPRORGB. What are the series of Hall of Fame memory: A: HOFEXTREME limited edition, HOFEXTREME, HOFPRORGB. These three memory modules all have relatively good performance. Among them, the HOFEXTREME limited edition has the best performance. Compared with the previous two models, the HOFPRORGB is slightly weaker but the performance is also very good. Hall of Fame memory expansion introduction: 1. Using Samsung B-die particles, which is the king of memory particles and has a long service life.

How to use LINQ to query data in C# How to use LINQ to query data in C# Oct 08, 2023 pm 12:24 PM

How to use LINQ to query data in C# requires specific code examples. LINQ (LanguageIntegratedQuery) is a powerful query language in C#, which can help developers simplify data query and operation. This article will introduce how to use LINQ to query data in C# and provide specific code examples. Before using LINQ to query data, we first need to define a data source. In the example of this article, we assume that there is a class named "Person",

iPhone 15 Pro and 15 Pro Max new improvements preview iPhone 15 Pro and 15 Pro Max new improvements preview Sep 12, 2023 pm 10:45 PM

Apple will launch four new iPhone models at 1 a.m. on September 13, namely iPhone 15, 15 Plus, 15 Pro and 15 Pro Max. The high-end iPhone 15 Pro and Pro Max will use a new design, and the side material will be changed from stainless steel to titanium. The colors of the Pro series include gray, black, dark blue, white and other options. The screens of iPhone 15 Pro and Pro Max will be slightly larger this year than last year’s 14 Pro and 14 Pro Max. This change is due to Apple's adoption of a new process technology called LIPO (low injection pressure overmolding), which reduces the thickness of the screen frame by about one-third. High-end models will switch to titanium with a matte texture

Print the fractional representation of the first N terms of the series (0.25, 0.5, 0.75,...) Print the fractional representation of the first N terms of the series (0.25, 0.5, 0.75,...) Sep 17, 2023 pm 10:53 PM

Input N, which is equal to the maximum number of series to be printed Input:N=5Output:0¼½¾1AlgorithmSTARTStep1->declarestartvariablesasintnum,den,i,nStep2->inputnumberinnStep3->LoopForfromito0andi<nandi++ OuterIfi%2=0 &nbsp

From HiFi sound quality to AI, the experience of Samsung Galaxy Buds3 series has been greatly upgraded From HiFi sound quality to AI, the experience of Samsung Galaxy Buds3 series has been greatly upgraded Aug 27, 2024 pm 03:30 PM

From ancient vinyl records, album tapes and CDs that were popular in the millennium, to today's streaming media platforms on mobile devices, with the development of the times, the carriers for listening to music are also constantly changing. With the support of advanced technology, people's pursuit of immersive experience continues to advance, and many novel technical means have been developed as a result. Among them, the new generation of TWS headsets represented by the Samsung Galaxy Buds 3 series have found new breakthroughs on the AI ​​track. High-fidelity sound quality HiFi pleasant listening experience As a new product focusing on sound quality experience, there are two new products in the Samsung Galaxy Buds3 series: 1. Equipped with 24bit/96kHz codec technology, which can bring studio-like high quality when connected to a Galaxy smartphone

See all articles