linq主要有3种,linq to sql,linq to XML,Linq to Object
linq to sql。
这里没有通过相应的类,生成相应的数据库中的表。没有用流行的编码优先。
只是为了自己的练习。
通过生成的linq 类,把数据库中的表,存储过程,视图等映射出来。其中数据上下文是链接实体类和数据库的桥梁,这是非常重要的。
现在开始Linq to sql之旅。数据库中的代码如下所示:
--查询数据库中是否含有数据库DB_Student,有则删除
if exists(select 1 from sys.sysdatabases where [name]='DB_Student')
drop database DB_Student
go
--创建学生是数据库
create database DB_Student
go
--查看是否存在学生表,有则删除
use DB_Student
if exists(select 1 from sys.sysobjects where [name]='T_Student')
drop table T_Student
go
--创建学生表
use DB_Student
create table T_Student
(
stuNumber varchar(8) not null,
stuName varchar(20) not null,
stuAge int not null,
stuSex char(2) not null
)
go
--添加主键约束
use DB_Student
alter table T_Student
add constraint PK_T_Student_stuNumber primary key(stuNumber)
go
--查看是否存在教师表,有则删除
use DB_Student
if exists(select 1 from sys.sysobjects where [name]='T_Teacher')
drop table T_Teacher
go
--创建教师表
use DB_Student
create table T_Teacher
(
teacherNumber varchar(6) not null,
teacherName varchar(20) not null
)
go
--为教师表创建主键约束
use DB_Student
alter table T_Teacher
add constraint PK_T_Teacher_teacherNumber primary key(teacherNumber)
go
--查看是否存在课程表,有则删除
use DB_Student
if exists(select 1 from sys.sysobjects where [name]='T_Cource')
drop table T_Cource
go
--创建课程表
use DB_Student
create table T_Cource
(
courceNumber varchar(4) not null,
courceName varchar(20) not null,
teacherNumber varchar(6) not null
)
go
--为课程表添加主键约束和外键约束
use DB_Student
alter table T_Cource
add constraint PK_T_Cource_courceNumber primary key(courceNumber),
constraint FK_T_Cource_T_Teacher_teacherNumber foreign key(teacherNumber) references T_Teacher(teacherNumber)
go
--查看是否存在成绩表,有则删除
use DB_Student
if exists(select 1 from sys.sysobjects where [name]='T_Score')
drop table T_Score
go
--创建成绩表
use DB_Student
create table T_Score
(
stuNumber varchar(8) not null,
courceNumber varchar(4) not null,
score float
)
go
--为成绩表添加主键约束和外键约束
use DB_Student
alter table T_Score
add constraint PK_T_Score_stuNumber_courceNumber primary key(stuNumber,courceNumber),
constraint FK_T_Score_T_Student_stuNumber foreign key(stuNumber) references T_Student(stuNumber),
constraint FK_T_Score_T_Cource_courceNumber foreign key(courceNumber) references T_Cource(courceNumber)
go
--为学生表插入数据
insert into T_Student
select '2091727','李阳','18','男' union
select '2091728','李芳','19','女' union
select '2091729','李丹','18','男' union
select '2091721','黄阳','17','女' union
select '2091722','黄渤','18','男' union
select '2091723','黄波','20','男' union
select '2091724','何洁','19','女' union
select '2091725','何丹','17','男' union
select '2091726','何宇','19','女'
go
--为教师表插入数据
insert into T_Teacher
select '01','李刚' union
select '02','李双江' union
select '03','邓楠'
go
--为课程表插入数据
insert into T_Cource
select '001','微机原理','01' union
select '002','西方音乐历史','02' union
select '003','会计学','03'
go
--为成绩表插入数据
insert into T_Score
select '2091721','001','92' union
select '2091721','002','59' union
select '2091721','003','81' union
select '2091722','001','58' union
select '2091722','002','66' union
select '2091722','003','83' union
select '2091723','001','82' union
select '2091723','002','64' union
select '2091723','003','71' union
select '2091724','001','77' union
select '2091724','002','53' union
select '2091724','003','56' union
select '2091725','001','55' union
select '2091725','002','57' union
select '2091725','003','71' union
select '2091726','001','90' union
select '2091726','002','88' union
select '2091726','003','82' union
select '2091727','001','57' union
select '2091727','002','83' union
select '2091727','003','59' union
select '2091728','001','54' union
select '2091728','002','93' union
select '2091728','003','48' union
select '2091729','001','61' union
select '2091729','002','58' union
select '2091729','003','93'
go
select *
from T_Cource
select *
from T_Score
select *
from T_Student
select *
from T_Teacher
通过数据库生成数据上下文开始在VS里编码:
public partial class Practice1 : System.Web.UI.Page
{
DB_StudentDataContext ctx = new DB_StudentDataContext();
protected void Page_Load(object sender, EventArgs e)
{
dataBind();
}
void dataBind()
{
//获取数据库中的T_Student表数据
var list = ctx.T_Student.ToList();
GridView1.DataSource = list;
GridView1.DataBind();
}
}
这就是后台代码,前台显示如下:
看只需要var list = ctx.T_Student.ToList();这样的一句代码,就能够绑定数据,和以前ADO.NET读取数据填充到dataset数据集相比省了很多代码,看是不是很方便。
开始where 练习
/**************where 练习*******************/
//获取数据库中的T_Student表数据
var list = ctx.T_Student.Where(s => s.stuName == "黄阳");
//或者
var list1 = from s in ctx.T_Student
where s.stuName == "黄阳"
select s;
//查询名字为"黄阳"且性别为女的同学
var list3 = ctx.T_Student.Where(s => (s.stuName == "黄阳")&&(s.stuSex=="女"));
var list4 = from s in ctx.T_Student
where s.stuName == "黄阳"
&& s.stuSex == "女"
select s;
var list5 = ctx.T_Student.Where(s => s.stuName == "黄阳").Where(t => t.stuSex == "女");
//上边三个都是一样的结果
//查询表中的第一条记录
var list6 = ctx.T_Student.First();
//查询名字为“黄阳”的第一条记录
var list7 = ctx.T_Student.First(s=>s.stuName=="黄阳");