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/
表名: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
- Unity之基础查询,连接数据库
- Unity学习之查询基础和连接数据库
- Unity之连接查询
- unity之连接数据库
- unity学习之连接数据库
- unity之连接SQLite数据库
- php基础之连接mysql数据库和查询数据
- 查询基础和连接数据库
- 数据库基础:连接查询总结
- unity学习之数据库基础
- unity3d---数据库基础查询和连接数据库
- Unity复习之SQL语句基础查询
- orcale数据库之连接查询
- 数据库查询之(Ⅲ)连接查询
- 数据库之联合查询和连接查询
- 查询基础以及数据库的连接
- [Unity基础]unity连接sqlServer2008
- Oracle数据库之SQL连接查询
- 第16周OJ-指针(3)
- 聊一聊重构这个磨人的小妖精
- Unity之列的基本操作和约束
- android之截屏(包括截取scrollview与listview的)
- Android Studio绝对实用的新手教程(1)导入第三方包或者运行eclipse项目
- Unity之基础查询,连接数据库
- 指针逆序输出数组
- 第16周OJ-指针(4)
- Android中ListView.getCount()与ListView.getChildCount()区别和OnScrollListener()各个参数的区别
- Huaman Gene Functions
- Unity3D之高级查询
- 虚函数底层实现机制
- 转载一篇LDA算法使用总结
- CSS盒模型--边框设置:border: 1px solid red(像素 样式 颜色 ),border-bottom:1px dotted #ccc