unity之增删改查

欢迎来到我们的 狗刨网 ,今天我们主要讲的是怎么把数据库放在项目里运行,实现增删改查,这是最基础的内容,首先我们只有掌握了这些才能够在

项目中完成用户的登陆和注册。

一、首先是数据的查

      class Program
      {
        List list=new List(); //创建列表
        public List select()
        {
            SqlConnection con=new SqlConnection("sever=.;database=GameMarket;Trusted_Connection"); //创建SqlConnection 对象
            con.Open();                                //打开连接
            string sql="select*from users";  //从显示数据库的所有信息
            SqlCommand cmd=new SqlCommand (sql,con); //创建 SqlCommand 对象
            SqlDataReader reader=cmd.ExecuteReader(); //获取SqlDataReader对象
            while(reader.Read())
            {
               users u=new users();
               u.Id=(int)reader.GetValue(0);   //获取id的值
               u.Name=(string)reader.GetValue(1);
                list.Add(u);
            }
            return list;
        }

二、数据的插入

       public int insert(string name) 
        {
            SqlConnection con = new SqlConnection("sever=.;database=GameMarket;trusted_Connection=SSPI");
            con.Open();
            string sql = "insert into users value('" + name + "')";
            SqlCommand cmd = new SqlCommand();
            int i = cmd.ExecuteNonQuery();
            return i;
        }

三、数据的修改

       public int update(string name,int id) 
        {
            SqlConnection con = new SqlConnection("sever=.;database=GameMarket;trusted_Connection=SSPI");
            con.Open();
            string sql = "update users set name='"+name+"'where id='"+id+"'";
            SqlCommand cmd = new SqlCommand(sql, con);
            int i = cmd.ExecuteNonQuery();
            return i;
        }

四、数据的删除

       public int delete(int id) 
        {
            SqlConnection con=new SqlConnection("sever=.;database=GameMarket;trusted_Connection=SSPI");
            con.Open();
            string sql = "delete from users where id='"+id+"'";
            SqlCommand cmd = new SqlCommand(sql,con);
            int i = cmd.ExecuteNonQuery();
            return  i;
        }
狗刨网每天都会更新新的内容,日日有更新,每天进步一点点收获多多,有更多内容详见我们的 狗刨网。

你可能感兴趣的:(unity之增删改查)