SQL语法和C#调用

1. SQL 关键字不区分大小写

         对象名和列名不区分大小写

         引用字符值区分大小写

         SQL语句可分布在多行上,以空格和换行符分隔

2. 查询所有列

         select * from 查找表名

在Select语句后指定列名

select 列名1,列名2,列名3  from 查找表名;

 

3. Where 语句

用于指定条件子句,当条件为TRUE,返回满足条件的数据,如果条件为FALSE,不返回数据。编写子句时合,需要使用各种比较操作符

 

 3.1 Where句子中使用数字

 可用单引号引住,也可直接使用

select * from fp_xs t where t.dj = 17;

select * from fp_xs t where t.dj = '17';

 

3.2  Where句子中使用字符

    必须用单引号引住

    Char数据类型存储数字时,可省略

         select * from dj_nsrxx t where t.nsrsbh = '530112734313217';

3.3  where 子句中使用is null

         select * from fp_yj t where t.je is null;

3.4  where 子句中使用in

  当列或表达式匹配列表中的任一值时,返回满足条件的值

   select * from 查找表名 where 查找列 in(值1,值2); 

3.5 where 子句中使用like  含糊查找

   %:表示0到多个字符

  _:  表示单个字符

  模糊查询中含有两个通配符时,需用escape指定转义字符

 select * from 查找表 t where查找列 like '%铜业%';  //表示在查找列中包含铜业的数据

3.6  where 子句中使用逻辑操作符

select * from 查找表 where 查找列 = '420'or查找列= '175'; //查找列等于420或者175

 

3.7  order by子句  指定结果如何排序:升序:ASC;降序:DESC

         Select * from student order by SNAME   

 

 

 

4. update语句   修改语句

UPDATE table SET newvalue WHERE criteria

// 把修改表中SNO=99对应 score的值修改成65

Update 修改表 set score=65 where sno= ‘99’

 

5. insert into语句

INSERT INTO <表名> (<字段列表>) values (<值列表>)

//将数值1,值2,值3赋值给插入行sno,cname,score

Insert into Curse (sno,cname,score) values (‘值1',’值2’,值3)

//将数值1,值2,值3赋值给插入行 (赋值必须给插入所有的值进行赋值)

Insert into Curse  values (‘值1',’值2’,值3)

 

6. delete from语句

         删除指定数据。

    delete from <表名> where <条件>

   //从名单中删除‘王五’

    Delete from student where sname= '王五'

         //将删除学生表中所有记录

    Delete from student

 

7.创建一个数据表

//myTable1为要添加的数据表,PKeyMyId1添加数据的索引,myIdmyNamemyAddressmyBalance为添加的添加的数据。

string sql = "CREATE TABLE myTable3" + "(myId INTEGER," +"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";

 

                   private void CreateData()

        {

            // 打开连接

            SqlConnectionStringBuilder SqlConStr = new SqlConnectionStringBuilder();

           

            SqlConStr.DataSource = ".";

            SqlConStr.InitialCatalog = "demo";

            SqlConStr.Password = "123456";

            SqlConStr.UserID = "sa";

            SqlConnection conn = new SqlConnection(SqlConStr.ToString());

            conn.Open();

            //myTable1为要添加的数据表,PKeyMyId1添加数据的索引,myIdmyNamemyAddressmyBalance为添加的添加的数据。

            string sql = "CREATE TABLE myTable3" + "(myId INTEGER," +"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";

            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.ExecuteNonQuery();

        }

//添加数据

private static void AddData(TextBox tbox)

        {

            //连接字符串内容构造

            SqlConnectionStringBuilder sqlConStr=new SqlConnectionStringBuilder();

            sqlConStr.DataSource = ".";         //连接数据源,如果是本机可以设置为'.'127.0.0.1

            sqlConStr.InitialCatalog = "demo"//数据库名称

            sqlConStr.UserID = "sa";            //用户ID

            sqlConStr.Password = "123456";      //用户密码

            SqlConnection sqlConn = new SqlConnection(sqlConStr.ToString());

            //直接

            //SqlConnection sqlConn = new SqlConnection("server=.;database=demo;user=sa;pwd=123456");

            sqlConn.Open();//打开连接

           

            string time = DateTime.Now.ToString();

            //需要执行的SQL语句   [demo].[dbo].[Table1]中,demo为数据库名称,dbo固定,Table1为表格名称

            //数据在()中用,隔开,数据用

            String sqlStr = "insert into [demo].[dbo].[Table1]  values ('yumu','12','34','88')";

 

            //创建用于执行sql语句的对象

            //参数1sql语句字符串,参数2:已打开的数据连接对象

            SqlCommand sqlComm = new SqlCommand(sqlStr, sqlConn);

            //执行SQL指令

             sqlComm.ExecuteNonQuery();

        }

 

        //查询数据

        private static void Query( ListBox Lbox)

        {

            //构造连接字符串

            SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();

            scsb.DataSource = ".";

            scsb.InitialCatalog = "demo";

            scsb.UserID = "sa";

            scsb.Password = "123456";

 

            //创建连接 参数为连接字符串

            SqlConnection sqlConn = new SqlConnection(scsb.ToString());

 

            //打开连接

            sqlConn.Open();

 

            //需要执行的SQL语句

            String sqlStr = "SELECT * FROM [demo].[dbo].[Table1] where name = 'Li'";

            //创建用于执行sql语句的对象

            //参数1sql语句字符串,参数2:已打开的数据连接对象

            SqlCommand sqlComm = new SqlCommand(sqlStr, sqlConn);

 

            //接收查询到的sql数据

            SqlDataReader reader = sqlComm.ExecuteReader();

 

            //读取数据

            while (reader.Read())

            {

                // 可以使用数据库中的字段名,也可以使用角标访问

                Lbox.Items.Add(reader["name"].ToString() + reader["age"].ToString());

            }

           

        }

        //删除数据

        private static void DelData(TextBox tbox)

        {

            //构造连接字符串

            SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();

            scsb.DataSource = ".";

            scsb.InitialCatalog = "demo";

            scsb.UserID = "sa";

            scsb.Password = "123456";

 

            //创建连接 参数为连接字符串

            SqlConnection sqlConn = new SqlConnection(scsb.ToString());

 

            //打开连接

            sqlConn.Open();

           

            //需要执行的SQL语句

            //"Delete from Categories where CategoryID="+TextBox1.Text;

            //String sqlStr = "insert into [demo].[dbo].[Table1] values  ('hhghh','8988')";

            String sqlStr = "delete from [demo].[dbo].[Table1] where name='yumu'";

            //创建用于执行sql语句的对象

            //参数1sql语句字符串,参数2:已打开的数据连接对象

            SqlCommand sqlComm = new SqlCommand(sqlStr, sqlConn);

             sqlComm.ExecuteNonQuery();

            //读取数据

           

        }

        //更新

        private static void UpdateData1()

        {

            //构造连接字符串

            SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();

            scsb.DataSource = ".";

            scsb.InitialCatalog = "demo";

            scsb.UserID = "sa";

            scsb.Password = "123456";

 

            //创建连接 参数为连接字符串

            SqlConnection sqlConn = new SqlConnection(scsb.ToString());

 

            //打开连接

            sqlConn.Open();

 

            //需要执行的SQL语句

            //"Delete from Categories where CategoryID="+TextBox1.Text;

            //String sqlStr = "insert into [demo].[dbo].[Table1] values  ('hhghh','8988')";

            String sqlStr = "update [demo].[dbo].[Table1] set age='99' where  name='Li'";

            //创建用于执行sql语句的对象

            //参数1sql语句字符串,参数2:已打开的数据连接对象

            SqlCommand sqlComm = new SqlCommand(sqlStr, sqlConn);

 

            sqlComm.ExecuteNonQuery();

 

            //读取数据

 

        }

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页