Uinty3D学习基础知识 数据库连接

来源:互联网 发布:java模拟http上传文件 编辑:程序博客网 时间:2024/06/07 13:29
Unity3D学习
  今天我们大家学习如何连接数据库
 
在连接数据库之前先认识一下访问语句
 1.查询全部的行和列

          select * from users

 

    2. 查询部分行

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

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

 

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

          select distinct name from users

 

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

          select id from users union select id from score

 

    5. 用AS来命名列

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

 

    6. 用 来命名列

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

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

 

    7.查询空行

         select id, name from users where password is null

 

    8.查询非空行

         select name from users where name is not null

 

    9.使用常量列(默认值)

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

 

    10.限制固定行数

         select top 3 * from users

 

   11. 返回百分之多少行

         select top 50 percent * from users

 

   12. 排序

        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


 
首先来看你下访问数据库的步骤:
 
1.开始
 
2.引入命名空间
 
using System.Data;
using System.Data.SqlClient;
 
注意:命名空间是必须要导入的。
 
3.创建一SQLConnection对象
 
4.打开连接(用Open()方法)
 
5.创建一SQLCommand对象
 
6.关闭连接(用Close()方法)
 
7.关闭SQLDataReader对象
 
8.获取SQLDataReader对象
 
9.结束
 
 
举个例子:
 
(访问数据库)
  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.         }
 
 
 
(在数据库中添加数据)
  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.         }
 
(查询功能)
需要建一个People类
 
 class People
    {
     public  string name;
        public void Setname(string name)
        {
            this.name = name;
        }
        public string Getname()
        {
            return name;
        }
     public  int age;
    
        public void Setage(int age)
        {
            this.age = age;
        }
        public int Getage()
        {
            return age;
        }
        public int id;
        public int Id
        {
            set { id = value; }
            get { return id; }
        }
    }
 

            public List<People> select()
            {
                List<People> list = new List<People>();

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

                SqlCommand com = new SqlCommand("select*from teacher",con);

                SqlDataReader myreader = com.ExecuteReader();

                while (myreader.Read())
                {
                    People  p = new People ();

                    p.Id=(int)myreader.GetValue(0);

                    p.Setname ((string)myreader.GetValue(1));

                    p.Getname();

                    p.Setage((int)myreader.GetValue(2));

                    p.Getage();

                    list.Add(p);

                }
                myreader.Close();
                con.Close();

                return list;

                foreach (People p in  list)
                {
                    Console.WriteLine(p.Getname()+" "+p.Getage()+" "+p.Id);
                }
            }
 
 static void Main(string[] agrs)
        {
            Class3 a = new Class3();

            a.select();

           a.Get();
 
 
           List<People> list = new Class3 (). select();

          IEnumerator<People> it = list.GetEnumerator();

           while (it.MoveNext())
           {
               Console.WriteLine(it.Current.Getage()+" "+it.Current.Getname ()+" "+it.Current.Id);
           }
 
 
 
更多精彩内容请到:http://www.gopedu.com/
 
 
0 0
原创粉丝点击