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

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了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 临时变量中被查询的值

例:

from c in Student select c;

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

则查询语句为:

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、排序

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、过滤重复记录

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)查询表的总行数

int count=student.count();

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

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

7、模糊查询

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类型进行接收

如:

var query=from c in Student select c;

输入结果可用foreach循环

如:

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();

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

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

studentDataContext dc = new studentDataContext();
//注意:xxxDataContext需要与.dbML的文件名一致
var query=from c in dc.student select c;
GridView1.DataSource=query;
GridView1.DataBind();

更新操作

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();//一更改,就显示,和及时刷新相同。

PRivate void bind()
{
studentDataContext dc = new studentDataContext();
 var query = (from c in dc.student select c); //全表查询
 GridView1.DataSource = query;
 GridView1.DataBind();
}

9、插入操作

//插入
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、数据删除

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相关内容感兴趣的读者可查看本站专题:《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

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

脚本宝典总结

以上是脚本宝典为你收集整理的asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析全部内容,希望文章能够帮你解决asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。