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 <排序的列名>[ASC或DESC]]
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里面用代码实现:
- public void Get()
- {
- SqlConnection con = new SqlConnection("server=.;database =users;Trusted_Connection=SSPI");
- SqlCommand cmd=new SqlCommand("select * from teacher",con);
- con.Open();
- SqlDataReader myreader=cmd.ExecuteReader();
- while (myreader.Read())
- {
- Console.WriteLine(myreader.GetValue(0) + " " +myreader.GetValue(1));
- }
- myreader.Close();
- con.Close();
- }
其中在创建 SqlConnection 对象时,大致有两种写法,大家可以根据自己数据库的情况来选择或更改
SqlConnection con = new SqlConnection("server=.;database=users;Trusted_Connection=SSPI");
SqlConnection con = new SqlConnection("server=服务器;database =users;uid=sa;pwd=ad";database=users);
接下来试试在表中插入数据的功能:
- public int insert()
- {
- SqlConnection con = new SqlConnection("server=.;database =users;Trusted_Connection=SSPI");
- string sql = "insert into teacher(name,age,id) values('赵六',23,114)";
- SqlCommand comm = new SqlCommand(sql, con);
- con.Open();
- int count = comm.ExecuteNonQuery();
- return count;
- }
最后来看看查询功能,这个前提要先建一个Person,实现封装:
- class Person
- {
- private string name;
- private int id;
- private int age;
- public void SetId(int id)
- {
- this.id = id;
- }
- public int GetId()
- {
- return id;
- }
- public void SetName(string name)
- {
- this.name = name;
- }
- public string GetName()
- {
- return name;
- }
- public void SetAge(int age)
- {
- this.age = age;
- }
- public int GetAge()
- {
- return age;
- }
- }
- public List<Person> select()
- {
- List<Person> list = new List<Person>();
- SqlConnection con = new SqlConnection("server=.;database =users;Trusted_Connection=SSPI");
- SqlCommand cmd = new SqlCommand("select * from users", con);
- con.Open();
- SqlDataReader myreader = cmd.ExecuteReader();
- while (myreader.Read())
- {
- Person p = new Person();
- p.SetId((int)myreader.GetValue(0));
- p.SetName((string)myreader.GetValue(1));
- p.SetAge((int)myreader.GetValue(2));
- list.Add(p);
- }
- myreader.Close();
- con.Close();
- return list;
- foreach (Person p in list)
- {
- Console.WriteLine(p.GetId()+" "+ p.GetName()+" "+p.GetAge());
- }
- }
- static void Main(string[] args)
- {
- List<Person > list =new Program().select();//实现查询功能
- IEnumerator<Person > it = list.GetEnumerator();
- while(it.MoveNext()){
- Console.WriteLine(it.Current.GetId() + " " + it.Current.GetName()+it.Current.GetAge());
- }
- Program p = new Program();
- p.Get();//实现连接数据库并显示表中内容
- p.insert();//实现在数据库中插入数据
- Console.ReadKey();
- }
这样就在vs中完成了对数据库的操作。
更多精彩请点击 http://www.gopedu.com/article
- Unity学习之查询基础和连接数据库
- Unity之基础查询,连接数据库
- unity学习之连接数据库
- unity学习之数据库基础
- 查询基础和连接数据库
- php基础之连接mysql数据库和查询数据
- unity3d---数据库基础查询和连接数据库
- Unity之连接查询
- 数据库之联合查询和连接查询
- unity之连接数据库
- Oracle数据库学习5之查询进阶:连接查询
- unity之连接SQLite数据库
- 数据库基础:连接查询总结
- 数据查询基础和C#连接数据库语句
- 数据库学习(三)----基础查询和关联查询
- unity学习之高级查询
- 数据库基础复习之左连接和右连接
- Android学习之用adb连接模拟器查询sqlite数据库
- 《Effective C++》学习笔记——条款20
- 鹅鹅鹅
- storm解除twitter4jar包依赖的方法
- asp.net弹出层扩展研究
- ios绘图教程(原文http://www.cocoachina.com/industry/20140115/7703.html)
- Unity学习之查询基础和连接数据库
- 1412101621-hd-排名
- fedora20升级21
- Language Specification for Blocks
- 大学数据结构(c++版) 王红梅版 疑惑——第二章
- Java初级工程师面试题大全
- 复数的加减计算...
- android----bluetooth
- notonlysuccess 线段树 高人推荐