美文网首页
Unity3D开发-C#语言进阶篇(服务器常用方法之增删改查)

Unity3D开发-C#语言进阶篇(服务器常用方法之增删改查)

作者: TonyWan_AR | 来源:发表于2016-12-15 19:47 被阅读342次
     class Program
        {
            static void Main(string[] args)
            {
                //数据库的增删改查
                string MySqlStr = "Database=vr88;server=localhost;User Id=root;Password=;";
    
                MySqlConnection mySqlcon = new MySqlConnection(MySqlStr);//连接数据库;
                mySqlcon.Open();
    
                //查
                string str = "select * from student where sid=3;";
                MySqlCommand mySqlCom = new MySqlCommand(str, mySqlcon);
                CheckedMySql(mySqlCom);
    
                //增
                str = "insert into student values(15,'神10',28,'女');";
                mySqlCom = new MySqlCommand(str, mySqlcon);
                IducMySql(mySqlCom);
    
                //删            
                str = "delete from student where sid=5;";
                mySqlCom = new MySqlCommand(str, mySqlcon);
                IducMySql(mySqlCom);
    
                // 改
                str = "update student set sage=33 where sid=9;";
                mySqlCom = new MySqlCommand(str, mySqlcon);
                IducMySql(mySqlCom);
    
                GetId(9);
    
                Console.ReadKey();
            }
    
            //(查)
            static private void CheckedMySql(MySqlCommand mySqlcom)
            {
                MySqlDataReader read = mySqlcom.ExecuteReader();
                try
                {
                    while (read.Read())
                    {
    
                        if (read.HasRows)//查找
                        {
                            Console.WriteLine("ID:{0} 姓名:{1} 年龄:{2} 性别:{3}", read.GetInt32(0), read.GetString(1), read.GetInt32(2), read.GetString(3));
                        }
    
                    }
    
                }
                catch (Exception e)
                {
    
                    throw e;
                }
                finally
                {
                    read.Close();
                }
    
    
    
            }
    
    
            //增 删 改 查
            static private void IducMySql(MySqlCommand mySqlcom)
            {
                try
                {
                    mySqlcom.ExecuteNonQuery();
                }
                catch (Exception e)
                {
    
                    throw e;
                }
    
            }
    
    
            //指定ID参数打印相关信息
            static private void GetId(int id)
            {
    
                string MySqlStr = "Database=vr88;server=localhost;User Id=root;Password=;";
                MySqlConnection mySqlcon = new MySqlConnection(MySqlStr);
                mySqlcon.Open();
                string str = "select * from student where sid=" + id;//注意 别把分号打上去;
                MySqlCommand mySqlCom = new MySqlCommand(str, mySqlcon);
                MySqlDataReader reader = mySqlCom.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
    
                        if (reader.HasRows)//查找
                        {
                            Console.WriteLine("ID:{0} 姓名:{1} 年龄:{2} 性别:{3}", reader.GetInt32(0), reader.GetString(1), reader.GetInt32(2), reader.GetString(3));
                        }
    
                    }
    
                }
                catch (Exception e)
                {
    
                    throw e;
                }
                finally
                {
                    reader.Close();
                }
            }
        }
    
    

    相关文章

      网友评论

          本文标题:Unity3D开发-C#语言进阶篇(服务器常用方法之增删改查)

          本文链接:https://www.haomeiwen.com/subject/appgmttx.html