复习:
1、 ado.net
SqlConnection con =new SqlConnection(constr);
sqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameter.AddRange();
con.Open();
cmd.
2、 SqlHelper
reader封装时不能关闭connection,不能用using,所以要加try…catch…
3、 资源管理器练习
在Update方法中为了避免刷新整个
//将指定的文件夹下的文本文件导入到ContentInfo表中
openFileDialog1.Multiselect = true;
openFileDialog1.Filter = “txt|*.txt”;
删除某类
(1) 获取当前类别Id
(2) 执行删除语句
(3) 从TreeView中删除
treeView1.SelectedNode
注意一点:在数据库更新后把treeview中也更新
4、ADO.NET结构
比喻:
DataAdapter也可以更新数据库
电话本管理器
RowEnter事件
dataGridView1.Rows[e.Row]
1、 禁用自动生成列
dataGridView1.AutoGenerate
一、 多条件搜索
(一) 拼接sql语句
1、 “where 1=1”(太弱):有些数据库执行操作时会遍历整个表
StringBuilder sql = new StringBuilder(“Select * from PhoneNum where 1=1”);
if(cboGroup.SelectedIndex != 0)
{
sql.Append(“ and ptypeid=” + cbo.Group.Text.Split(‘|’)[0]);
}
if(txtSearchName.Text.Trim().Length > 0)
{
sql.Append(“ and pname like ‘%”+txtSearchName.Text.Trim()+”%’”);
}
2、使用List集合来拼接条件
StringBuilder sql = new StringBuilder(“Select * from PhoneNum”);
List<string> wheres = new List<string>();
if(cboGroup.SelectedIndex != 0)
{
wheres.Add(“ptypeid=” + cboGroup.Text.Split(‘|’)[0]);
}
if(txtSearchName.Text.Trim().Length > 0)
{
wheres.Add(“ and pname like ‘%”+txtSearchName.Text.Trim()+”%’”);
}
if(txtSearchCellPhone.Text.Trim().Length > 0)
{
sql.Append(“ and pcellphone like ‘%”+txtSearchCellPhone.Text.Trim()+”%’”);
}
//判断用户是否选择了条件
if(wheres.Count > 0)
{
string wh = string.Jion(“ and ”,wheres.ToArray());
sql.Append(“ where ” + wh);
}
(二)使用带参数的sql语句
//List<string> wheres = new List<string>();
//List<SqlParameter> listParameter = new List<SqlParameter>();
//增加一个list<SqlParameter>
//模糊查询带参数的sql语句,%一般写在替换参数的字符串中
if(cboGroup.SelectedIndex != 0)
{
wheres.Add(“ ptypeid=@typeid ”);
listParameter.Add(new SqlParameter(“@typeid”,cboGroup.Text.Split(‘|’)[0]));
}
if(txtSearchName.Text.Trim().Length > 0)
{
wheres.Add(“ pname like @pname ”);
listParameter.Add(new SqlParameter(“@pname”,”%”+txtSrearchName.Text.Trim()+”%”));
}
if(txtSearchCellPhone.Text.Trim().Length>0)
{
wheres.Add(“ pcellphone like @cellphone ”);
listParameter.Add(new SqlParameter(“@cellphone”,”%”+txtSearchCellPhone.Text,Trim()+”%”));
}
//判断用户是否选择了条件
if(where.Count > 0)
{
string wh = string.Jion(“ and ”,wheres.ToArray());
sql.Append(“ where ”+wh);
}
SqlHelper.ExecuteDataTable(sql.ToString(),listParameter.ToArray());//ToArray()可以把List集合转换为数组
二、case…when…语句
(一)写法一:
select
tscoreId,
tsid,
tenglish,
评级=
case
when tenglish >= 95 then ‘优秀’
when tenglish >=90 then ‘良好’
when tenglish >=80 then ‘中’
else ‘以后的比尔’
end
from TblScore
(二) 写法二:
select
tscoreId,
tsid,
tenglish,
评级=
case tenglish
when 100 then ‘百分’
when 90 then ‘九十分’
end
from TblScore
三、索引Index
全表扫描:对数据进行检索(select)效率最差的是全表扫描,就是一条一条找。
聚集索引(聚簇索引):数据实际存储顺序与索引顺序一致。例:字典按拼音查找
非聚集索引(非聚簇索引):数据实际存储顺序与索引顺序不一致。例:字典按部首查找
--非聚集索引
create nonclustered index IX_SalesPerson_SalesQuota_SalesYTD on Sales.SalesPerson(SalesQuota,SalesYTD);Go
--创建唯一非聚集索引
create unique index AK_UnitMeasure_Name ON Production.UnitMeasure(Name);Go
--创建聚集索引
create table t1 (a int, b int,c AS a/b);
create unique clustered index Idx1 on t1(c); insert into ti values (1,0);
--删除索引
drop index T8.IX_T*_tage
四、子查询
把一个查询结果作为另一个查询的查询源
把一个查询结果作为另一个查询的输出项
把一个查询结果作为另一个查询的查询条件
--查询所有“高二三班”与“高二一班”的学生的信息
--子查询中=、!=、<、<=、>、>=之后只能返回单个值,如果多个值就报错了。
--解决方法:把“=”改为“in”
select * from TblStudent
where tsclassid=(select tclassid from TblClass where tclassname=’高二三班’ or tclassname =’高二一班’)
l exists语句
if(exists(select * from tblstudent))—exists在这里判断true或false
begin
print ‘有结果’
end
else
begin
print ‘没结果’
end
以下两种查询结果相同:
select * from TblStudent
where tsclassid = (select tclassid from TblClass where tclassname=’高二三班’)
--相关子查询
select * from tblStudent
where
exists—exists在这里代表一个集合
{
select * from TblClass
where tclassname=’高二二班’ and TblClass.tclassid=TblStudent.tsclassid
}
五、分页
--row_number()行序
select *, row_number() over(order by fid desc) as rnumber from MyStudent
--查找第20页的数据
(
select * ,row_number() over (order by fid desc) as rnumber
from MyStudent
) as tbl
where tbl.rnumber between 19*5+1 and 20*5
六、Join 语句
1、 内联接(两个表中相匹配的记录)
select
ts.tsname, --两张表中不重复的字段可以不加“ts.”或“tc.”
ts.tsage,
tc.tclassname
from TblStudent as ts
inner join TblClass as tc on ts.tsclassid=tc.tclassid
2、 外联接
左外联接、右外联接:先将匹配的找出来,不匹配的加null
3、自连接
ts.tId,
ts.tName as 子分类,
td.tName as 父分类
from Category as ts
inner join (select tId,tName from Category) as td on ts.tParentId=td.tId
查询范围:join<子查询<exists