欢迎光临散文网 会员登陆 & 注册

DBheiper

2023-04-07 11:19 作者:海鸥在观音葫芦串  | 我要投稿

经典导入:
 //获取数据的连接信息
           String strcon = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
           //连接数据库
           SqlConnection con = new SqlConnection(strcon);
            //打开数据库
           con.Open();
           //写sql语句
           String sql = "select * from sex";
           //创建command对象
           SqlCommand com = new SqlCommand(sql,con);
           //执行sql
           SqlDataReader r = com.ExecuteReader();
           //临时仓库
           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.获取数据的连接信息
           String conStr = String.Format("Data Source=.;Initial Catalog={0};Integrated Security=True", dataBase);
           //2.创建连接对象
           SqlConnection con = new SqlConnection(conStr);
           //3.打开数据库
           con.Open();
           //4.写sql语句

           //5.创建command对象
           SqlCommand com = new SqlCommand(sql, con);
           //6.执行sql
           SqlDataReader r = com.ExecuteReader();
           return r;
       }
       //增删改
       public static int Update(String sql, String dataBase)
       {

           //1.获取数据的连接信息
           String conStr = String.Format("Data Source=.;Initial Catalog={0};Integrated Security=True", dataBase);
           //2.创建连接对象
           SqlConnection con = new SqlConnection(conStr);
           //3.打开数据库
           con.Open();
           //4.写sql语句

           //5.创建command对象
           SqlCommand com = new SqlCommand(sql, con);
           //6.执行sql
           int flag = com.ExecuteNonQuery();
           return flag;
       }
       //登录,最大值,最小值,个数,平均值
       public static int Scalar(String sql, String dataBase)
       {

           //1.获取数据的连接信息
           String conStr = String.Format("Data Source=.;Initial Catalog={0};Integrated Security=True", dataBase);
           //2.创建连接对象
           SqlConnection con = new SqlConnection(conStr);
           //3.打开数据库
           con.Open();
           //4.写sql语句

           //5.创建command对象
           SqlCommand com = new SqlCommand(sql, con);
           //6.执行sql
           int flag = (int)com.ExecuteScalar();
           return flag;
       }
//==================================================================================


//clr 自动刷新 方法

    //字符串
           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);
           //新建临时数据库
           ds = new DataSet();
           //填充
           sda.Fill(ds, "region");
           this.dgv.DataSource = ds.Tables["region"];







// Sql 语句

select * from Demo

--获取系统时间
select GETDATE()

--读取表里的生日
select birthady from Demo

--计算年龄
select DATEDIFF(yy,'2003-06-01',GETDATE())

--读取表里的生日                      -- as 别名
select DATEDIFF(yy,birthady,GETDATE()) as '计算结果' from Demo;  --如果没有from 表格 那么就无法定位到 名为 birthady的列 所以就会失败
-- 查询 [DATAEIFF() 函数返回两个日期的间隔 里面是 birthady日期 和 GETDATE 当前日期获取] 来自 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 = ''


-- 检索 所有 学生表 as 别名Stu
select * from Student as Stu
inner join score as Sco -- inner join 关联 成绩表 别名 as Sco
on Stu.Sid = Sco.id -- 关联条件 学生表的 Sid 和 成绩表的 id 进行关联 查询

--挑选结果
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 '学生年龄和' from Student

--最大值 最小值
select SUM(age) as '学生年龄和' from Student
select avg(age) as '学生平均值' from Student
select max(age) as '男同学的最大年龄'
from score where sex = '男'

--个数
select COUNT(*) as '男同学个数'  -- 一般来说括号里是 * 或者是 1
from score where sex = '男'

select max(age) as '男同学的最大年龄'
from score where sex = '男'
----------------
select count(*) as '女同学的个数'
from score where sex = '女'
----------------
select distinct(age) as '男同学的年龄都有那些'
from score where sex = '男'

--聚合函数 分组
select  sex,COUNT(*) as '个数' from score -- group by 为分组判断 他会把不一样的分组判断 计算个数出来 其中 group by 只负责分组 而 count负责计算
group by sex

--where  是分组前的条件
--having 是分组后的条件
select  sex,COUNT(*) as '个数' from score
where age > 1 -- 为前
group by sex
having COUNT(*) < 1 -- 为后
--运行结果 满足此条件为 0

select * from Usersinfo
where LoginName in ('lilei@163.com')

--使用连接查询登录名为 lilei@163.com 的用户所记笔记的笔记标题 内容和用户姓名
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'

--使用聚合函数查询登录名为 zhangtao@163.com 的用户且笔记分类名称是 工作 的笔记记录数
--聚合函数 分组
select  CategoryName,COUNT(*) as '个数' from NoteCategory -- group by 为分组判断 他会把不一样的分组判断 计算个数出来 其中 group by 只负责分组 而 count负责计算
group by CategoryName

--使用模糊查询 查询登录名为 lilei.163.com 的用户 且笔记标题里包含 邮件 的笔记标题和内容
select * from Usersinfo
where LoginName like '%lilei.163.com%'

--按照创建时间降序排序 查询登录名为 lilei@163.com 的前10条笔记的标题和内容


--查询所有用户的姓名 登录名 笔记标题和笔记内容


--查询记录笔记数大于 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)
--运行结果失败,是因为不允许为NULL 不然则可以

--多行插入
insert into student values
(4,5,6,7,8), --如果报红要加一个逗号
(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,'长春市')

select * from Patient;

--更新语句
UPDATE Patient set Password='11111'

--删除语句  范围是 id = 1;
delete from Patient where Patient=1;
--直接删除全部
delete from Patient
--但是序号都在

--用清除会连序号全部彻底删除

--去重 加入 distinct后 sex展示相同的只会显示一次
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 = "";


DBheiper的评论 (共 条)

分享到微博请遵守国家法律