string cmdtxt = “SELECT 产品编号,产品名称,产品单价,产品数量,(产品数量*产品单价) AS 总金额 FROM tb_03 where len(产品名称)=2”;
SqlDataAdapter dap = new SqlDataAdapter(“SELECT 所在部门, SUM(CASE 员工姓名 WHEN ‘李金明’ THEN 销售业绩 ELSE NULL END) AS [李金明],SUM(CASE 员工姓名 WHEN ‘周可人’ THEN 销售业绩 ELSE NULL END)as [周可人] ,SUM(CASE 员工姓名 WHEN ‘韩运’ THEN 销售业绩 ELSE NULL END)AS [韩运],SUM(CASE 员工姓名 WHEN ‘司徒南’ THEN 销售业绩 ELSE NULL END)AS [司徒南],SUM(CASE 员工姓名 WHEN ‘史佳金’ THEN 销售业绩 ELSE NULL END)AS [史佳金] FROM 销售表 GROUP BY 所在部门”, con);
SqlDataAdapter dap = new SqlDataAdapter(“SELECT 员工姓名, SUM(CASE 所在部门 WHEN ‘食品部’ THEN 销售业绩 ELSE NULL END) AS [食品部业绩],SUM(CASE 所在部门 WHEN ‘家电部’ THEN 销售业绩 ELSE NULL END) AS [家电部业绩],SUM(CASE 所在部门 WHEN ‘服装部’ THEN 销售业绩 ELSE NULL END) AS [服装部业绩] FROM 销售表 GROUP BY 员工姓名”, con);
string sqlstr = “Select 书名,季度 = Case when month(日期) in (1,2,3) then ‘1季度’ “;
sqlstr += ” when month(日期) in (4, 5, 6) then ‘2季度’ “;
sqlstr += ” when month(日期) in (7, 8, 9) then ‘3季度’ “;
sqlstr += ” when month(日期) in (10, 11, 12) then ‘4季度’ end ,”;
sqlstr += ” SUM(销售数量) as 销售数量 “;
sqlstr += ” from [tb_xsb] “;
sqlstr += ” where year(日期)=” + numericUpDown1.Value.ToString();
sqlstr += ” group by [书名], Case when month(日期) in (1, 2, 3) then ‘1季度’ “;
sqlstr += ” when month(日期) in (4, 5, 6) then ‘2季度’ “;
sqlstr += ” when month(日期) in (7, 8, 9) then ‘3季度’ “;
sqlstr += ” when month(日期) in (10, 11, 12) then ‘4季度’ end “;
sqlstr += ” order by [书名],[季度]”;
SqlDataAdapter dap = new SqlDataAdapter(sqlstr, cn);
SqlDataAdapter dap = new SqlDataAdapter(“SELECT [员工姓名], [出生日期], DateDiff(year,[出生日期],getDATE()) AS 年龄 FROM 员工生日表”, con);
string QueryStr = “SELECT * FROM tb_08 WHERE 备注 IS null OR 备注=””;
SqlDataAdapter dap = new SqlDataAdapter(“select 出版社,sum(金额) as 合计金额 from tb_xsb group by 出版社 order by 2 desc”, conn);
SqlDataAdapter dap = new SqlDataAdapter(“SELECT DISTINCT 所属部门, COUNT(*) AS 部门人数, MAX(基本工资) AS 最高工资, AVG(基本工资) AS 平均工资 FROM 部门工资统计表 GROUP BY 所属部门 HAVING (AVG(基本工资) > 1000)”, conn);
SqlDataAdapter dap = new SqlDataAdapter(“SELECT a.所属部门, MAX(a.基本工资) AS 部门最高工资,SUM(b.请假天数) AS 合计请假天数, AVG(b.扣除金额) AS 平均扣除金额 FROM 部门工资统计表 a INNER JOIN 员工请假表 b ON a.员工编号 = b.员工编号 GROUP BY a.所属部门 HAVING SUM(b.请假天数) < 10”, conn);
SqlDataAdapter dap = new SqlDataAdapter(“SELECT 员工信息表.员工编号, 员工信息表.员工姓名 FROM 员工信息表 INNER JOIN 员工工资表 ON 员工信息表.员工编号 = 员工工资表.员工编号 order by 员工工资表.员工编号”, conn);
SqlDataAdapter dap = new SqlDataAdapter(“SELECT * FROM 明细工资表 WHERE (员工编号 IN (” + textBox1.Text + “))”, conn);
SqlDataAdapter dap = new SqlDataAdapter(“select distinct 学生姓名,学生编号, 性别,出生年月,年龄,所在学院,所学专业 from tb_stu where 学生姓名 in (select 学生姓名 from tb_mark where 总分>=580)”, conn);
SqlDataAdapter dap = new SqlDataAdapter(“select * from [工资数据表] where 工资月份=10 and 人员姓名 in ( select 负责人 from 部门表 where 负责人 in (select 人员姓名 from 人员表 where 学历=’本科’)) order by 人员编号”, conn);
switch (Condition)
{
case “%like%”://左右模糊查询
{
StrSQL += ” where ” + FieldName + ” like ‘%” + FieldValue + “%'”;//组合SQL查询语句
blur = true;
break;
}
case “%like”://左模糊查询
{
StrSQL += ” where ” + FieldName + ” like ‘%” + FieldValue + “‘”;//组合SQL查询语句
blur = true;
break;
}
case “like%”://右模糊查询
{
StrSQL += ” where ” + FieldName + ” like ‘” + FieldValue + “%'”;//组合SQL查询语句
blur = true;
break;
}
SqlDataAdapter dap = new SqlDataAdapter(“select distinct 书号,条形码,书名,作者,出版社 from tb_xsb”, My_con);
SqlDataAdapter dap = new SqlDataAdapter(“select Count(书号) as 销售笔数, 书号,书名,作者 from tb_xsb group by 书号,书名,作者 Having Count(书号)>1”, My_con);
SqlDataAdapter dap = new SqlDataAdapter(“select distinct 书号,书名,作者,销售数量,日期 from tb_xsb order by 书号 asc,日期 desc “, conn);
string SQLStr = “select top 5 书号,书名,作者,出版社,sum(销售数量) as 合计销售数量 from tb_xsb group by 书号,书名,作者,出版社 order by 合计销售数量 desc”;
SqlDataAdapter dap = new SqlDataAdapter(“SELECT A.序号, A.员工编号, A.员工姓名, B.月份, B.基本工资, B.奖金 FROM 员工信息表 A LEFT OUTER JOIN 明细工资表 B ON A.员工编号 = B.员工编号”, con);
SqlDataAdapter dap = new SqlDataAdapter(“SELECT A.编号, A.薪资编号, B.员工编号, A.基本工资, A.奖金, B.员工姓名, B.身份证号 FROM 员工信息表 B RIGHT OUTER JOIN 明细工资表 A ON B.员工编号 = A.员工编号”, con);
SqlDataAdapter dap = new SqlDataAdapter(“SELECT A.序号, A.员工编号, A.员工姓名, B.薪资编号, B.月份, B.基本工资, B.奖金, C.请假天数, C.扣除金额 FROM 员工信息表 A LEFT OUTER JOIN 明细工资表 B ON A.员工编号 = B.员工编号 LEFT OUTER JOIN 员工请假表 C ON A.员工编号 = C.员工编号 “, con);
SqlDataAdapter dap = new SqlDataAdapter(“select avg(年龄) as 平均年龄 from tb_stu”, conn);
string QueryString = “select top 10 percent 书号,书名,sum(销售数量) as 合计销售数量 from tb_xsb group by 书号,书名,作者 order by 3 asc”;
SqlDataAdapter dap = new SqlDataAdapter(“select 考生编号,姓名,考生类别 From 高考学生信息表 where 籍贯=’中国北京’ UNION select 考生编号,姓名,考生类别 from 高考成绩表 where 总成绩 > 500 AND 考生类别=’文科考生'”, conn);
SqlDataAdapter dap = new SqlDataAdapter(“select 姓名 From 学生信息表 UNION select 课程名称 From 课程表 where 课程名称=’计算机英语’ UNION select convert(varchar(20),成绩) from 学生成绩表 where 成绩 > 90 UNION Select convert(varchar(20),出勤率) From 学生考勤表 where 出勤率 > 0.8”, conn);
SQLStr = “SELECT cast(成绩 AS varchar(20)) AS 成绩 FROM 学生成绩表 UNION SELECT DISTINCT 课程编号 FROM 学生信息表 UNION SELECT 课程名称 FROM 课程表 WHERE 课程名称 = ‘计算机英语’ UNION SELECT CONVERT(varchar(20), 出勤率) FROM 学生考勤表 WHERE 出勤率 > 0.8 ORDER BY 成绩 DESC”;
//1、精确查询
//string SearchStr = “select * from tb_stu where 学生姓名='” + tbName.Text.Trim() + “‘”;
//2、模糊查询
string SearchStr = “select * from tb_stu where 学生姓名 like ‘%” + tbName.Text.Trim() + “%'”;
string MyDate = dateTimePicker1.Value.ToString(“yyyy-MM-dd”);
SqlDataAdapter dap = new SqlDataAdapter(“select * from tb_stu where 出生年月='” + MyDate + “‘”, cn);
1、利用“_”通配符进行查询按钮事件
string sqlstr = “select * from tb_stu where 学生编号 like ‘” + textBox1.Text.Trim() + “_'”;
SqlDataAdapter dap = new SqlDataAdapter(“select * from tb_stu where 学生编号 like ‘%” + textBox2.Text + “%'”, cn);
SqlDataAdapter dap = new SqlDataAdapter(“select * from tb_stu where 年龄 like ‘” + textBox3.Text.Trim() + “[0-9]'”, cn);
SqlDataAdapter dap = new SqlDataAdapter(“select * from tb_stu where 年龄 like ‘[^” + textBox4.Text + “][0-9]'”, cn);
string sqlstr = “select * from tb_stu where 学生编号 like ‘”
+ textBox5.Text + “%’ and ( 学生姓名 like ‘”
+ textBox6.Text + “_’ or 年龄 like ‘[^”
+ textBox7.Text + “][0-9]’)”;
SqlDataAdapter dap = new SqlDataAdapter(sqlstr, cn);