C# 数据库SQl语句

    xiaoxiao2021-03-25  136

    1。空置处理 ISNULL 2、分组 group by select Classid as 班级id, 班级人数=Count(*) from TblStudent group by ClassID 当时用了分组语句(group by)或者是聚合函数的时候,在select的查询列表中不能包含其他列名除非这列也包含在分组语句中,或者写一个聚合函数(Count) 使用having 对分组以后的数据进行筛选 having 与where都是对数据进行筛选,where对分组前的每行数据筛选,having对分组后的每行数据筛选 select Classid as 班级id, 班级人数=Count(*) from TblStudent group by ClassID having Count(*)>1 order by 班级人数 asc 语句执行顺序 from 表 where 条件 Group by 列 Having筛选条件 Order by 列 3、类型转换函数 select 100+200 select 100+'1000' print 'a'+100 不行哦 cast() convert() select 100.0+cast('1000' as int ) OK select 100.0+convert(int,'1000') 4、union 联合结果集(集合运算符) 把多个结果集中的行联合起来 union 和union all 都能进行联合 区别: union会去除重复,重新排列数据 union all 不会去除重复也不重新排列 大多数情况下联合的时候不需要保持数据的顺序,所以一般建议使用union all 使用union向表中插入多条数据 insert into Tblstudent select XXXXX union all select XXXXX union all select XXXXX union all select XXXXX union all 5、向表中插入多条记录 select * from Tble11 from Tblstudent 新表中没有以前表的约束 6、字符串函数 1、len()计算字符的个数 print len(‘Hi-最近好吗? ’) datalength()返回所占用字节的个数,这个不是字符串函数 2、upper 转换大写 lower()转换小写 3、去掉两端空格ltrim rtrim 4、字符串截取函数 left() right() substring()截取字符串 substring(XXXXX,1,3) 从几开始,截取几个。 7、日期函数 getdate() sysdatetime() dateadd(day,200,getdate()) DATEDIFF ( datepart , startdate , enddate )计算两个日期的差 获取日期的某部分值 year(getdate()) month(getdate()) datepart(datepart,getdate()) 8、ADO.NET Connection 连接数据库 Command 执行SQL语句 DataReader 只读、只进的结果集,一条条读取数据。 DataAdapter 封装了上面的三个对象 DataSet 临时数据库 //连接数据库步骤 //1.创建连接字符串 //Data Source=ASUS; //Initial Catalog=Honest; //Integrated Security=True string constr = "Data Source=ASUS;INITIAL Catalog=study; Integrated Security=True"; //2.创建连接对象 //相当于TRY Finaly using (SqlConnection con = new SqlConnection(constr)) { //测试,打开连接 //3。打开连接 con.Open(); Console.WriteLine("打开成功"); //4.关闭连接,释放资源 } Console.WriteLine("关闭连接,释放资源!!!"); Console.ReadKey(); DataReader特点: 1、只读、 只进(不能后退).只能通过reader读取数据,不能修改。reader只能一条条向前移动,不能后退 2、使用reader时必须保证连接是打开状态。reader使用完毕后必须关闭,释放。同时关闭释放连接对象 3、reader默认情况下要求 独占一个连接对象。 网数据库插入值返回自动编号: insert into Tbclass output inserted.tClassId valus('dsadsa','dsadas') 9.防止SQL注入: 使用带参数的SQL语句或者存储过程。 string sql = string.Format("SELECT COUNT(*) FROM dbo.[User] WHERE u_id=@user AND u_pwd=@password");
    转载请注明原文地址: https://ju.6miu.com/read-4940.html

    最新回复(0)