黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

Linq無聊練習系列1--where練習

系統 3082 0

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

這就是后臺代碼,前臺顯示如下:

Linq無聊練習系列1--where練習

看只需要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=="黃陽");

Linq無聊練習系列1--where練習


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論