asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析

2025-05-29 0 112

本文实例讲述了asp.net使用LINQ to SQL连接数据库SQL操作语句用法。分享给大家供大家参考,具体如下:

LINQ简介

LINQ:语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据。

LINQ是一门查询语言,和SQL一样,通过一些关键字的组合,实现最终的查询。

LINQ的分类

LINQ to Object
LINQ to XML
LINQ to SQL
LINQ to DataSet
LINQ to ADO.NET

命名空间为System.Linq;

LINQ查询

语法:
from 临时变量 in 集合对象或数据库对象
  where 条件表达式
  [orderby条件]
  [group by 条件]
  select 临时变量中被查询的值

例:

?

1
from c in Student select c;

假设Student是一个数据库表对应的一个实体类

则查询语句为:

?

1

2

3

4
from c in Student select c;

//整表查询

from c in Student where c.name=="张三" select c;

//查询姓名为张三的所有信息

其中C为临时变量,可任意取。

查询几个字段

1、查询student表中的几个字段

复制代码 代码如下:

var query=from c in student select new {c.number,c.name,c.age};

2、查询student表中的几个字段,并重新设定列名

复制代码 代码如下:

var query=from c in student select new {学号=c.number,姓名=c.name, 年领=c.age};

注意事项

linq查询语句必须以from子句开始,以select 子句结束。

Linq是在.NET Framework 3.5 中出现的技术,所以在创建新项目的时候必须要选3.5或者更高版本,否则无法使用。

3、排序

?

1

2
var query=from c in student orderby c.age ascending select c;//升序

var query=from c in studeng orderby c.age descending select c;//降序

4、分组

复制代码 代码如下:

var query=from c in student group c by c.sex into d select new {性别=c.age}; //d为新表,c.sex为分组字段

5、过滤重复记录

?

1

2
var query=(from c in dc.student select new {c.place}).Distinct();//Distinct()的作用是过滤重复的记录。

var query=(from c in dc.student select new {分布地区=c.place}).Distinct();

6、查询行数

(1)查询表的总行数

?

1
int count=student.count();

(2)查询满足条件的行数

?

1
int count=(from c in student where c.name=="王明" select c).count();

7、模糊查询

?

1
from c in dc.Student where c.name.Contain("王") select c

查询姓名中含有王字的所有学生

复制代码 代码如下:

var query=from c in dc.Student where c.number.Contain("2009") select c

查询学号中含有2009字符的所有学生

查询结果

LINQ的查询结果有可能是一个对象,也有可能是一个数据集,可用var类型进行接收

如:

?

1
var query=from c in Student select c;

输入结果可用foreach循环

如:

?

1

2

3
var query=from c in Student select c;

foreach( var x in query)

{ Response.Write(x.toString());}

常用函数

Count( ):计算查询结果的行数
Distinct( ):对查询结果的重复行进行筛选
First( ):取得查询结果的第一行
Last( ):取得查询结果的最后一行
Take(n):取得查询结果的前n行
Skip(n):略过前n行,从n+1行开始取
Skip(m).Take(n):从m+1行开始取后面的n行

8、更新操作

思路:先把需要更新的行查询出来,然后进行更新。LINQ只需要写出查询语句即可,不需要写更新语句!

例:将学生表中学号为00001的学生进行更新

1、(from c in Stuent where c.id=="00001" select c).First();

在数据空间中显示数据查询结果:

前两行是连接数据库,其中第一中,经常用,可以放到最开始,这样就不必每次用到时都写了。

?

1

2

3

4

5
studentDataContext dc = new studentDataContext();

//注意:xxxDataContext需要与.dbml的文件名一致

var query=from c in dc.student select c;

GridView1.DataSource=query;

GridView1.DataBind();

更新操作

?

1

2

3

4

5

6

7

8

9

10
string num = TextBox2.Text.Trim(); //将要更新学号为多少的相关信息

string name = TextBox3.Text.Trim();//更新的姓名

int age = Convert.ToInt32(TextBox4.Text.Trim());//Int32整型 //更新的年龄

StudentDataContext dc=new StudentDataContext();

student stu=(from c in dc.student where c.number==num select c).First();//变量,选取第一行。where后根据主键来更新,其他字段不能。即通过获取主键后,来更新其他字段。

//除过主键不修改外,其他字段都可以修改

stu.name = name;//将新修改的名字赋值给数据库中的字段名name

stu.age = age;//修改年龄

dc.SubmitChanges();//真正的用于修改数据库。

bind();//一更改,就显示,和及时刷新相同。

?

1

2

3

4

5

6

7
private void bind()

{

studentDataContext dc = new studentDataContext();

var query = (from c in dc.student select c); //全表查询

GridView1.DataSource = query;

GridView1.DataBind();

}

9、插入操作

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
//插入

string num = TextBox1.Text.Trim();

string username = TextBox2.Text.Trim();

string sex = TextBox3.Text.Trim();

string place = TextBox4.Text.Trim();

int age = Convert.ToInt32(TextBox5.Text.Trim());

student stu = new student();//创建对象

//赋新值

//主键不能重复

stu.number = num;

stu.name = username;

stu.sex = sex;

stu.place = place;

stu.age = age;

dc.student.InsertOnSubmit(stu);

//对表studen表进行插入操作。

//注意,该函数必须写正确。

dc.SubmitChanges();//数据库保存

bind();//内容和上面的相同

10、数据删除

?

1

2

3

4

5

6
string num = TextBox6.Text.Trim();

student stu =(from c in dc.student where c.number == num select c).First();

dc.student.DeleteOnSubmit(stu);

//删除数据库中的字段,具体怎样删除不管,只管调用该函数即可。

dc.SubmitChanges();

bind();

希望本文所述对大家asp.net程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析 https://www.kuaiidc.com/100580.html

相关文章

发表评论
暂无评论