Unity学习之查询基础和连接数据库

来源:互联网 发布:李兴华java实战经典 编辑:程序博客网 时间:2024/06/09 00:20

unity学习笔记,希望对喜欢unity的朋友有所帮助


什么是查询

      查询产生一个虚拟表,看到的是表形式显示的结果,但结果并不真正存储,每次执行查询只是现从数据表中提取数据,并按照表的形式显示出来

 

数据查询-基础语句


     查询全部的行和列

          select * from users

 

     查询部分行

          select id,name from users where name='张三'

          select id,name from users where name<>'张三'

 

     去掉重复字段查询记录

          select distinct name from users

 

     合并查询(合并两表中相同的字段)所查字段要类型相同

          select id from users union select id from score

 

     用AS来命名列

          select id as 编号,name as 姓名 from users

 

     用 来命名列

          select 编号 =id ,姓名=name  from users

          select '编号' =id ,'姓名'=name  from users

 

    查询空行

         select id, name from users where password is null

 

    查询非空行

         select name from users where name is not null

 

    使用常量列(默认值)

         select name as  姓名 ,'密码' as password from users

 

    限制固定行数

         select top 3 * from users

 

    返回百分之多少行

         select top 50 percent * from users

 

    排序

        SELECT <列名>  FROM   <表名>  [WHERE    <查询条件表达式>] [ORDER BY <排序的列名>[ASCDESC]]

        asc:升序  desc:降序

        升序

            select * from users order by id

            select * from users order by id asc

       降序

            select * from users order by id desc

 

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

         select * from users order by name,id


连接数据库


     程序访问数据库的步骤


          

 

下面我们通过代码来实践把,当然前提要保证数据库里要有建好的数据库和表哦


首先访问数据库要先引入

using System.Data;

using System.Data.SqlClient;

 

然后我们根据访问数据库的步骤在vs里面用代码实现:


  1.         public void Get()
  2.         {
  3.             SqlConnection con = new SqlConnection("server=.;database =users;Trusted_Connection=SSPI");
  4.             SqlCommand cmd=new SqlCommand("select * from teacher",con);
  5.             con.Open();
  6.             SqlDataReader myreader=cmd.ExecuteReader();
  7.             while (myreader.Read())
  8.             {
  9.                 Console.WriteLine(myreader.GetValue(0) + "   " +myreader.GetValue(1));   
  10.             }
  11.             myreader.Close();
  12.             con.Close(); 
  13.         }

其中在创建 SqlConnection 对象时,大致有两种写法,大家可以根据自己数据库的情况来选择或更改

SqlConnection con = new SqlConnection("server=.;database=users;Trusted_Connection=SSPI");

SqlConnection con = new SqlConnection("server=服务器;database =users;uid=sa;pwd=ad";database=users);


接下来试试在表中插入数据的功能:


  1. public int insert()
  2.         {
  3.             SqlConnection con = new SqlConnection("server=.;database =users;Trusted_Connection=SSPI");
  4.             string sql = "insert into teacher(name,age,id) values('赵六',23,114)";
  5.             SqlCommand comm = new SqlCommand(sql, con);
  6.             con.Open();
  7.             int count = comm.ExecuteNonQuery();
  8.             return count;
  9.         }


最后来看看查询功能,这个前提要先建一个Person,实现封装:


  1. class Person
  2.     {
  3.         private string name;
  4.         private int id;
  5.         private int age;
  6.         public void SetId(int id)
  7.         {
  8.             this.id = id;
  9.         }
  10.         public int GetId()
  11.         {
  12.             return id;
  13.         }
  14.         public void SetName(string name)
  15.         {
  16.             this.name = name;
  17.         }
  18.         public string GetName()
  19.         {
  20.             return name;
  21.         }
  22.         public void SetAge(int age)
  23.         {
  24.             this.age = age;
  25.         }
  26.         public int GetAge()
  27.         {
  28.             return age;
  29.         }
  30.     }

  1. public List<Person> select()
  2.         {
  3.             List<Person> list = new List<Person>();
  4.             SqlConnection con = new SqlConnection("server=.;database =users;Trusted_Connection=SSPI");
  5.             SqlCommand cmd = new SqlCommand("select * from users", con);
  6.             con.Open();
  7.             SqlDataReader myreader = cmd.ExecuteReader();
  8.             while (myreader.Read())
  9.             {
  10.                 Person p = new Person();
  11.                 p.SetId((int)myreader.GetValue(0));
  12.                 p.SetName((string)myreader.GetValue(1));
  13.                 p.SetAge((int)myreader.GetValue(2));
  14.                 list.Add(p);
  15.             }
  16.             myreader.Close();
  17.             con.Close();
  18.             return list;
  19.             foreach (Person p in list)
  20.             {
  21.                 Console.WriteLine(p.GetId()+" "+ p.GetName()+"   "+p.GetAge());
  22.             }
  23.         }

最后当然不要忘了在Main 方法里面调用,还有连接数据库和插入的调用也一并写在一起

  1.         static void Main(string[] args)
  2.         {
  3.             List<Person > list =new Program().select();//实现查询功能
  4.             IEnumerator<Person > it = list.GetEnumerator();
  5.             while(it.MoveNext()){
  6.                 Console.WriteLine(it.Current.GetId() + "  " + it.Current.GetName()+it.Current.GetAge());
  7.             }
  8.             Program p = new Program();
  9.             p.Get();//实现连接数据库并显示表中内容
  10.             p.insert();//实现在数据库中插入数据
  11.             Console.ReadKey();
  12.         }


这样就在vs中完成了对数据库的操作。


更多精彩请点击 http://www.gopedu.com/article

0 0
原创粉丝点击