DBheiper
經(jīng)典導(dǎo)入:
?//獲取數(shù)據(jù)的連接信息
? ? ? ? ? ?String strcon = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
? ? ? ? ? ?//連接數(shù)據(jù)庫
? ? ? ? ? ?SqlConnection con = new SqlConnection(strcon);
? ? ? ? ? ? //打開數(shù)據(jù)庫
? ? ? ? ? ?con.Open();
? ? ? ? ? ?//寫sql語句
? ? ? ? ? ?String sql = "select * from sex";
? ? ? ? ? ?//創(chuàng)建command對(duì)象
? ? ? ? ? ?SqlCommand com = new SqlCommand(sql,con);
? ? ? ? ? ?//執(zhí)行sql
? ? ? ? ? ?SqlDataReader r = com.ExecuteReader();
? ? ? ? ? ?//臨時(shí)倉庫
? ? ? ? ? ?ds = new DataSet();
? ? ? ? ? ?sda = new SqlDataAdapter(sql, strcon);
? ? ? ? ? ?sda.Fill(ds,"emg");
? ? ? ? ? ?this.dzy.DataSource = ds.Tables["emg "];
//工具類:
//==============================================================================
//查詢
? ? ? ?public static SqlDataReader GetDataReader(String sql, String dataBase)
? ? ? ?{
? ? ? ? ? ?//1.獲取數(shù)據(jù)的連接信息
? ? ? ? ? ?String conStr = String.Format("Data Source=.;Initial Catalog={0};Integrated Security=True", dataBase);
? ? ? ? ? ?//2.創(chuàng)建連接對(duì)象
? ? ? ? ? ?SqlConnection con = new SqlConnection(conStr);
? ? ? ? ? ?//3.打開數(shù)據(jù)庫
? ? ? ? ? ?con.Open();
? ? ? ? ? ?//4.寫sql語句
? ? ? ? ? ?//5.創(chuàng)建command對(duì)象
? ? ? ? ? ?SqlCommand com = new SqlCommand(sql, con);
? ? ? ? ? ?//6.執(zhí)行sql
? ? ? ? ? ?SqlDataReader r = com.ExecuteReader();
? ? ? ? ? ?return r;
? ? ? ?}
? ? ? ?//增刪改
? ? ? ?public static int Update(String sql, String dataBase)
? ? ? ?{
? ? ? ? ? ?//1.獲取數(shù)據(jù)的連接信息
? ? ? ? ? ?String conStr = String.Format("Data Source=.;Initial Catalog={0};Integrated Security=True", dataBase);
? ? ? ? ? ?//2.創(chuàng)建連接對(duì)象
? ? ? ? ? ?SqlConnection con = new SqlConnection(conStr);
? ? ? ? ? ?//3.打開數(shù)據(jù)庫
? ? ? ? ? ?con.Open();
? ? ? ? ? ?//4.寫sql語句
? ? ? ? ? ?//5.創(chuàng)建command對(duì)象
? ? ? ? ? ?SqlCommand com = new SqlCommand(sql, con);
? ? ? ? ? ?//6.執(zhí)行sql
? ? ? ? ? ?int flag = com.ExecuteNonQuery();
? ? ? ? ? ?return flag;
? ? ? ?}
? ? ? ?//登錄,最大值,最小值,個(gè)數(shù),平均值
? ? ? ?public static int Scalar(String sql, String dataBase)
? ? ? ?{
? ? ? ? ? ?//1.獲取數(shù)據(jù)的連接信息
? ? ? ? ? ?String conStr = String.Format("Data Source=.;Initial Catalog={0};Integrated Security=True", dataBase);
? ? ? ? ? ?//2.創(chuàng)建連接對(duì)象
? ? ? ? ? ?SqlConnection con = new SqlConnection(conStr);
? ? ? ? ? ?//3.打開數(shù)據(jù)庫
? ? ? ? ? ?con.Open();
? ? ? ? ? ?//4.寫sql語句
? ? ? ? ? ?//5.創(chuàng)建command對(duì)象
? ? ? ? ? ?SqlCommand com = new SqlCommand(sql, con);
? ? ? ? ? ?//6.執(zhí)行sql
? ? ? ? ? ?int flag = (int)com.ExecuteScalar();
? ? ? ? ? ?return flag;
? ? ? ?}
//==================================================================================
//clr 自動(dòng)刷新 方法
? ? //字符串
? ? ? ? ? ?String StrCon = "Data Source=.;Initial Catalog=cartoondata;Integrated Security=True";
? ? ? ? ? ?SqlConnection con = new SqlConnection(StrCon);
? ? ? ? ? ?con.Open();
? ? ? ? ? ?//sql語句
? ? ? ? ? ?String sql_qqw = String.Format(@"select book_name,book_class,book_zuozhe from book where book_class like '%{0}%' ",clr_name);
? ? ? ? ? ?sda = new SqlDataAdapter(sql_qqw, con);
? ? ? ? ? ?//新建臨時(shí)數(shù)據(jù)庫
? ? ? ? ? ?ds = new DataSet();
? ? ? ? ? ?//填充
? ? ? ? ? ?sda.Fill(ds, "region");
? ? ? ? ? ?this.dgv.DataSource = ds.Tables["region"];
// Sql 語句
select * from Demo
--獲取系統(tǒng)時(shí)間
select GETDATE()
--讀取表里的生日
select birthady from Demo
--計(jì)算年齡
select DATEDIFF(yy,'2003-06-01',GETDATE())
--讀取表里的生日 ? ? ? ? ? ? ? ? ? ? ?-- as 別名
select DATEDIFF(yy,birthady,GETDATE()) as '計(jì)算結(jié)果' from Demo; ?--如果沒有from 表格 那么就無法定位到 名為 birthady的列 所以就會(huì)失敗
-- 查詢 [DATAEIFF() 函數(shù)返回兩個(gè)日期的間隔 里面是 birthady日期 和 GETDATE 當(dāng)前日期獲取] 來自 Demo 表格
--全部查詢
select * from student
--條件查詢
select name,age from student
where age>30
order by age desc
--起別名
? ? ? ? ? ? ? ? --注意,as 可寫可不寫
select name as '姓名' ,age as '年齡' ?from student
where age>30
order by age desc
--加top
select top 2 ?name as '姓名' ,age as '年齡' ?from student
where age>30
order by age desc
--加常量列
select top 2 ?name as '姓名' ,age as '年齡' , '你好' as '還好' ?from student
where age>30
order by age desc
--加百分比查詢 top
select top 50 percent ?name as '姓名' ,age as '年齡' , '你好' as '還好' ?from student
where age>30
order by age desc
--加百分比查詢 加減操作
select top 50 percent ?name as '姓名' ,age ? 50 as '年齡' , '你好' as '還好' ?from student
where age>30
order by age desc
-- is NULL 和 為空不一樣
-- 查詢條件為 值為NULL 或者 值為'空'
select * from student
where phone is null or phone = ''
-- 檢索 所有 學(xué)生表 as 別名Stu
select * from Student as Stu
inner join score as Sco -- inner join 關(guān)聯(lián) 成績(jī)表 別名 as Sco
on Stu.Sid = Sco.id -- 關(guān)聯(lián)條件 學(xué)生表的 Sid 和 成績(jī)表的 id 進(jìn)行關(guān)聯(lián) 查詢
--挑選結(jié)果
select Stu.name,Sco.name,Sco.age,Sco.sex,Sco.id from Student as Stu
inner join score as Sco
on Stu.Sid = Sco.id
--還可以寫條件
where Sco.age=20
--------------
select * from score as Sco -- 查詢
left join Student as Stu ? -- 左邊查詢 沒有顯示NULL
on Stu.Sid = Sco.id
--右鏈接 取反
--求和
select SUM(age) as '學(xué)生年齡和' from Student
--最大值 最小值
select SUM(age) as '學(xué)生年齡和' from Student
select avg(age) as '學(xué)生平均值' from Student
select max(age) as '男同學(xué)的最大年齡'
from score where sex = '男'
--個(gè)數(shù)
select COUNT(*) as '男同學(xué)個(gè)數(shù)' ?-- 一般來說括號(hào)里是 * 或者是 1
from score where sex = '男'
select max(age) as '男同學(xué)的最大年齡'
from score where sex = '男'
----------------
select count(*) as '女同學(xué)的個(gè)數(shù)'
from score where sex = '女'
----------------
select distinct(age) as '男同學(xué)的年齡都有那些'
from score where sex = '男'
--聚合函數(shù) 分組
select ?sex,COUNT(*) as '個(gè)數(shù)' from score -- group by 為分組判斷 他會(huì)把不一樣的分組判斷 計(jì)算個(gè)數(shù)出來 其中 group by 只負(fù)責(zé)分組 而 count負(fù)責(zé)計(jì)算
group by sex
--where ?是分組前的條件
--having 是分組后的條件
select ?sex,COUNT(*) as '個(gè)數(shù)' from score
where age > 1 -- 為前
group by sex
having COUNT(*) < 1 -- 為后
--運(yùn)行結(jié)果 滿足此條件為 0
select * from Usersinfo
where LoginName in ('lilei@163.com')
--使用連接查詢登錄名為 lilei@163.com 的用戶所記筆記的筆記標(biāo)題 內(nèi)容和用戶姓名
select Note.Title,Note.Content,Usr.UserName from Usersinfo as Usr
inner join NoteInfo as Note
on Usr.UserId = Note.NoteId
where Usr.LoginName = 'lilei@163.com'
--使用聚合函數(shù)查詢登錄名為 zhangtao@163.com 的用戶且筆記分類名稱是 工作 的筆記記錄數(shù)
--聚合函數(shù) 分組
select ?CategoryName,COUNT(*) as '個(gè)數(shù)' from NoteCategory -- group by 為分組判斷 他會(huì)把不一樣的分組判斷 計(jì)算個(gè)數(shù)出來 其中 group by 只負(fù)責(zé)分組 而 count負(fù)責(zé)計(jì)算
group by CategoryName
--使用模糊查詢 查詢登錄名為 lilei.163.com 的用戶 且筆記標(biāo)題里包含 郵件 的筆記標(biāo)題和內(nèi)容
select * from Usersinfo
where LoginName like '%lilei.163.com%'
--按照創(chuàng)建時(shí)間降序排序 查詢登錄名為 lilei@163.com 的前10條筆記的標(biāo)題和內(nèi)容
--查詢所有用戶的姓名 登錄名 筆記標(biāo)題和筆記內(nèi)容
--查詢記錄筆記數(shù)大于 1 的用戶姓名 登錄名和性別
use school
select * from student;
--添加語句
insert into student values(2,3,4,5,6,7);
--添加語句
insert into student (name,sex,age)
values
(4,5,6)
--運(yùn)行結(jié)果失敗,是因?yàn)椴辉试S為NULL 不然則可以
--多行插入
insert into student values
(4,5,6,7,8), --如果報(bào)紅要加一個(gè)逗號(hào)
(5,6,7,8,9)
insert into Patient (password,BirthDate,Gender,PatientName,PhoneNum,Email,IndentityNum,Address)
values(123456,'1985-06-07','女','夏穎',13800000001,'ying.xia@qq.com',110000198506071100,'廈門市'),
(234567,'1985-06-08','男','李政',1380000002,'lizheng@163.com',210000198506082100,'長(zhǎng)春市')
select * from Patient;
--更新語句
UPDATE Patient set Password='11111'
--刪除語句 ?范圍是 id = 1;
delete from Patient where Patient=1;
--直接刪除全部
delete from Patient
--但是序號(hào)都在
--用清除會(huì)連序號(hào)全部徹底刪除
--去重 加入 distinct后 sex展示相同的只會(huì)顯示一次
select distinct sex from Student
//下拉選
? ? ? ? ? ?com[0] = -1;
? ? ? ? ? ?com[1] = "全部";
? ? ? ? ? ?ds.Tables[0].Rows.InsertAt(com, 0);
? ? ? ? ? ?this.com.DataSource = ds.Tables[0];
? ? ? ? ? ?this.com.DisplayMember = "";
? ? ? ? ? ?this.com.ValueMember = "";