日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Linq無聊練習(xí)系列1--where練習(xí)

系統(tǒng) 2948 0

linq主要有3種,linq to sql,linq to XML,Linq to Object

linq to sql。

這里沒有通過相應(yīng)的類,生成相應(yīng)的數(shù)據(jù)庫中的表。沒有用流行的編碼優(yōu)先。

只是為了自己的練習(xí)。

通過生成的linq?類,把數(shù)據(jù)庫中的表,存儲(chǔ)過程,視圖等映射出來。其中數(shù)據(jù)上下文是鏈接實(shí)體類和數(shù)據(jù)庫的橋梁,這是非常重要的。

現(xiàn)在開始Linq to?sql之旅。數(shù)據(jù)庫中的代碼如下所示:

--查詢數(shù)據(jù)庫中是否含有數(shù)據(jù)庫DB_Student,有則刪除
if exists(select 1 from sys.sysdatabases where [name]='DB_Student')
?? ?drop database DB_Student
go
--創(chuàng)建學(xué)生是數(shù)據(jù)庫
create database DB_Student
go
--查看是否存在學(xué)生表,有則刪除
use DB_Student
if exists(select 1 from sys.sysobjects where [name]='T_Student')
?? ?drop table T_Student
go
--創(chuàng)建學(xué)生表
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
--創(chuàng)建教師表
use DB_Student
create table T_Teacher
(
?? ?teacherNumber varchar(6) not null,
?? ?teacherName varchar(20) not null
)
go
--為教師表創(chuàng)建主鍵約束
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
--創(chuàng)建課程表
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?? ??? ?

--查看是否存在成績(jī)表,有則刪除
use DB_Student
if exists(select 1 from sys.sysobjects where [name]='T_Score')
?? ?drop table T_Score
go

--創(chuàng)建成績(jī)表
use DB_Student
create table T_Score
(
?? ?stuNumber varchar(8) not null,
?? ?courceNumber varchar(4) not null,
?? ?score float
)
go

--為成績(jī)表添加主鍵約束和外鍵約束
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?? ?

--為學(xué)生表插入數(shù)據(jù)
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

--為教師表插入數(shù)據(jù)
insert into T_Teacher
?? ?select '01','李剛' union
?? ?select '02','李雙江' union
?? ?select '03','鄧楠'
go
--為課程表插入數(shù)據(jù)
insert into T_Cource
?? ?select '001','微機(jī)原理','01' union
?? ?select '002','西方音樂歷史','02' union
?? ?select '003','會(huì)計(jì)學(xué)','03'
go
--為成績(jī)表插入數(shù)據(jù)?? ?
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

?

通過數(shù)據(jù)庫生成數(shù)據(jù)上下文開始在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()
??????? {
??????????? //獲取數(shù)據(jù)庫中的T_Student表數(shù)據(jù)
??????????? var list = ctx.T_Student.ToList();
??????????? GridView1.DataSource = list;
??????????? GridView1.DataBind();
??????? }
??? }

這就是后臺(tái)代碼,前臺(tái)顯示如下:

Linq無聊練習(xí)系列1--where練習(xí)

看只需要var list = ctx.T_Student.ToList();這樣的一句代碼,就能夠綁定數(shù)據(jù),和以前ADO.NET讀取數(shù)據(jù)填充到dataset數(shù)據(jù)集相比省了很多代碼,看是不是很方便。

開始where 練習(xí)

? /**************where 練習(xí)*******************/
??????????? //獲取數(shù)據(jù)庫中的T_Student表數(shù)據(jù)
??????????? var list = ctx.T_Student.Where(s => s.stuName == "黃陽");
??????????? //或者
??????????? var list1 = from s in ctx.T_Student
??????????????????????? where s.stuName == "黃陽"
??????????????????????? select s;
??????????? //查詢名字為"黃陽"且性別為女的同學(xué)
??????????? 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 == "女");
??????????????? //上邊三個(gè)都是一樣的結(jié)果

????   //查詢表中的第一條記錄
??????????? var list6 = ctx.T_Student.First();
??????????? //查詢名字為“黃陽”的第一條記錄
??????????? var list7 = ctx.T_Student.First(s=>s.stuName=="黃陽");

Linq無聊練習(xí)系列1--where練習(xí)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。。?/p>

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 贞丰县| 阆中市| 四川省| 城步| 沙田区| 民权县| 鄢陵县| 山阳县| 融水| 蒲江县| 重庆市| 历史| 江孜县| 循化| 安阳县| 伊金霍洛旗| 丹江口市| 高清| 平远县| 杭锦旗| 大竹县| 册亨县| 高台县| 浮梁县| 阿拉尔市| 资溪县| 松潘县| 凌云县| 上思县| 罗城| 刚察县| 抚顺县| 龙游县| 年辖:市辖区| 共和县| 高州市| 淳安县| 缙云县| 正安县| 肇庆市| 天祝|