Unity之基础查询,连接数据库

来源:互联网 发布:multisim 网络标号 编辑:程序博客网 时间:2024/06/05 07:09
1.基础查询

表名:work  ,play  列名: id,name,password

1.查询全部的列和行

select *from work 

2.查询部分

select id,name  from work   where id=101

3.去掉重复字段查询记录

select distinct name from work

4.合并查询(合并两表之间的相同字段)

select * from work union select *from play

5.用AS来命名列

select  id as  编号, name as 名字 from work

6.用"="来命名列

select  编号=id, 名字=name from  work

7.查询空行

select  name from work where name is null

8.使用常量列

select  '编号'  as  id,  '姓名'  as  name  from work

9.限制固定行数

select  top  3 *  from  work

10.返回百分之多少行

select  top  50  percent  *  from  work

2.排序

1.升序

select  *  from  work  order by  id  asc

2.降序

select *from  work  order by id desc

3.按多列排序(当排序值相同时,按第二个字段排序)

select * from work order by id , name

3.连接数据库

1.步骤:


开始----引入命名空间----创建一个SqlConnection对象----打开连接----创建一个SqlCommand对象----获取SqlDataReader对象----------关闭SqlDataReader对象----关闭连接----结束

引入命名空间:using System.Data;
                        using System.Data.SqlClient
class BD
    {
        public void Get() {
            SqlConnection con = new SqlConnection("server=.;Trusted_Connection=SSPI;database=first");
            //创建一个SqlConnection对象,打开连接
            SqlCommand cmd = new SqlCommand("select * from students,con");
           //创建一个SqlCommand对象
            con.Open();
            SqlDataReader myreader = cmd.ExecuteReader();
            //获取SqlDataReader对象
            while (myreader.Read()) {
                Console.WriteLine(myreader.GetValue(0) + " " + myreader.GetValue(1));
            }
            myreader.Close();
           //关闭SqlDataReader对象
            con.Close();
            //关闭连接
        }
        static void Main(string[] args) {
            new BD().Get();
        }

2.插入功能:

public void Insert() {
            SqlConnection con = new SqlConnection("server=.;Trusted_Connection=SSPI;database=first");
            SqlCommand cmd = new SqlCommand("insert into work (name,password) values ('zhangsan','123456789'),con");
            con.Open();
            int count = cmd.ExecuteNonQuery();
       }

3.查询功能:

public List<People> select() {
            List<People> list = new List<People>();
            SqlConnection con = new SqlConnection("server=.;Trusted_Connection=SSPI;database=first");
            con.Open();
            SqlCommand cmd = new SqlCommand("select *from students",con);
            SqlDataReader myread = cmd.ExecuteReader();
            while (myread.Read()) {
                People p = new People();
                p.SetName(""+myread.GetValue(0));
                p.GetName();
                p.SetPassword((string)myread.GetValue(1));
                p.GetPassword();
                list.Add (p);
            }
            myread.Close();
            con.Close();
            return list;
        }

4.调用查询功能:

static void Main(string[] args)
        {           
             IEnumerator<People> it = new BD().select().GetEnumerator();
             while (it.MoveNext())
             {
                 Console.WriteLine(it.Current.GetName() + "  " +
                  it.Current.GetPassword());
             }
             Console.ReadKey();
        }

 更多精彩关注:http://www.gopedu.com/
0 0
原创粉丝点击