SQL基本操作 查询 排序 函数 ADO.net

来源:互联网 发布:美食软件哪个好 编辑:程序博客网 时间:2024/05/22 08:05

SQL数据增删改

一、select数据查询

二、insert 数据插入

三、update 数据更新 

四、delete 数据删除

五、truncate 重新初始化表

六、练习 

use MySchool

 

select * from Teacher

insert into Teacher(tId, tName, tSxe, tAge, tSalary, tBirthday)

values(001,'苏坤','',33,10000,'1978-08-19')

insert into Teacher(tId, tName, tSxe, tAge, tSalary, tBirthday)

values(002,'杨中科','',39,15000,'1970-01-28')

delete from Teacher where tsalary>10000

truncate table Teacher

 

select * from Score

insert into Score(scoreId, studentId, english, math)

values(010,00001,90,100)

update Score set english=english+5,math=math+5

where scoreId=10

约束 constraint

一、主键约束 primary key constraint

二、唯一约束 unique constraint

三、默认约束 default constraint

四、检查约束 check constraint

五、外键约束 foreign constraint

六、多个约束

补充要点:

先建表,后建约束!

把建表和添加约束的代码分开来写,条理更清晰一些,也方便修改。

Not null 可以在建表时写上

建表时设置的约束名有随机字符,不方便控制

七、删除约束 

一般不修改约束,只新建或删除后再重建约束

3课 查询 query 

一、as 列别名

二、where条件

三、select输出显示 

四、top 获取前几条数据

五、distinct 去除重复数据

统计年龄段或年龄分布

六、聚合函数

 带条件的查询

一、带条件的查询

二、模糊查询-通配符

三、百分号%通配符

四、下划线_通配符

五、中括号[]通配符 

六、18位身份证验证

七、空值处理 null 

 1.null 

2.is null 

3.is not null

数据排序order by 

一、用order by 排序

二、多列带条件 order by 排序

6课 数据分组 group by

一、不使用分组方法进行

二、用group by 对数据分组  

运行结果

三、having对结果进行筛选

四、注意where having 区别

7课 联合查询

一、简单的结果集联合 union

二、union all 不去重

三、总结练习题 

问题 What’s this

 一次插入多条数据

一、插入数据  insert

二、插入到新表 

三、插入到已存在的表

2课 字符串函数

一、字符串函数

1.charindex 字符串位置

2.Len 字符串长度

3.Lower 转小写

4.Upper 转大写 

5.Ltrim Rtrim 去空格

6.Right 从右取

7、Left 从左取

自己试 

8、Replace 替换 

9、Stuff

10Substring

二、日期函数 

1.GetDate

2.DateAdd

3.DateDiff

4.DateName

5.DatePart

6.补充

三、练习

类型转换函数

一、Cast

二、Convert

三、补充实例  

空值处理函数

数学函数

ADO.net(一组类库)

一、ado.net主要类  

二、数据集DataSet 

三、connection 

还可以使用菜单连接数据库 

四、Command

 作业

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace _02Command

{

    public partial class Form1 : Form

    {

        string connString = @"data source=.;user id=sa;password=ffjj5910;Initial catalog=hotel";

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            using (SqlConnection conn = new SqlConnection(connString))

            {

                //SqlCommand cmd = new SqlCommand();

                //cmd.Connection = conn;

                //cmd.CommandText = string.Format("insert into class values('{0}','{1}')",text Box1.Text,textBox2.Text);

                string sql = string.Format("insert into class values('{0}','{1}')",textBox1.Text,textBox2.Text);

                SqlCommand cmd=new SqlCommand(sql,conn);

                try

                {

                    conn.Open();

                    int i = cmd.ExecuteNonQuery();

                    MessageBox.Show(i.ToString());

                

                }

                catch(Exception ex)

                {

                    MessageBox.Show("数据插入失败!原因:"+ex.Message);

                }

 

                //

            }

 

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            string sql = "delete from class where cid=" + textBox3.Text;

            using(SqlConnection conn=new SqlConnection(connString))

            {

                using(SqlCommand cmd=new SqlCommand(sql,conn))

                {

                    try

                    {

                        conn.Open();

                         

                        int i = cmd.ExecuteNonQuery();

                        if (i > 0)

                        {

                            MessageBox.Show("删除成功!");

                        }

                        else

                        {

                            MessageBox.Show("没有符合条件的数据!");

                        }

                

                    }

                    catch(Exception ex)

                    {

                        MessageBox.Show("删除失败!原因:"+ex.Message);

                    }

                }

            }

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            string sql = string.Format("update class set cname='{0}',cdescription='{1}' where cid='{2}'",txtname.Text,txtdes.Text,txtcid.Text);

            using(SqlConnection conn=new SqlConnection(connString))

            {

                using(SqlCommand cmd=new SqlCommand())

                {

                    cmd.Connection = conn;

                    cmd.CommandText = sql;

                    try

                    {

                        conn.Open();

                        cmd.ExecuteNonQuery();

                        MessageBox.Show("修改成功!");

                    }

                    catch(Exception ex)

                    {

                        MessageBox.Show("修改失败!原因:"+ex.Message);

                    }

                }

            }

        }

    }

原创粉丝点击