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

本文介绍了Unity中进行基础数据库查询的方法,包括查询全部和部分数据、去除重复记录、合并表查询、排序以及连接数据库的步骤。示例涉及SQL语句如SELECT、UNION、ORDER BY等,并展示了如何在C#中执行SQL操作,如插入、查询数据及读取结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值