Navicat for MySQL 数据库应用 (四)

using MySql.Data.MySqlClient;
using System.Collections.Generic;

namespace MySQTest
{
    public static class MySQLManager
    {
        //这里是数据库连接地址           地址 本机         用户        密码                          数据库名字                           
        public static string SqlPath = "Server=localhost;user id=root;Password=chuxinhao1314521;database=userdata";
        public static MySqlConnection mycon = new MySqlConnection(SqlPath);

        /// <summary>
        /// 向数据库创建一个用户信息
        /// </summary>
        /// <param name="Name">昵称</param>
        /// <param name="Account">帐号</param>
        /// <param name="Password">密码</param>
        /// <param name="Age">年龄</param>
        /// <param name="Gende">性别</param>
        /// <param name="Gold">金币</param>
        /// <param name="Level">等级</param>
        /// <param name="Phone">电话</param>
        public static bool Register(string Name, string Account, string Password, string Age, string Gende, string Gold, string Level, string Phone, out string message)
        {
            System.Console.WriteLine("数据库开始写入数据");
            message = "";

            try
            {
                mycon.Open();
                                        //此处注意符号问题不要写错
                string str = "insert into user(Name,Account,Password,Age,Gender,Gold,Level,Phone)values('" + Name + "','" + Account + "','" + Password + "','" + Age + "','" + Gende + "','" + Gold + "','" + Level + "','" + Phone + "')";
                MySqlCommand mycomm = new MySqlCommand(str, mycon);
                mycomm.CommandTimeout = 2;
                mycomm.ExecuteNonQuery();
                message = "注册成功";
                mycon.Close();
                System.Console.WriteLine("数据库写入成功");
                return true;
            }
            catch (System.Exception)
            {
                message = "服务器异常请关闭从新登录";
                System.Console.WriteLine("数据库写入失败,MySQL写入异常");
                return false;
            }

        }

        /// <summary>
        /// 删除一个用户
        /// </summary>
        /// <param name="account">用户帐号</param>
        public static void DeleteUser(string account)
        {
            mycon.Open();
            string str = "delete from user where Account='" + account + "'";
            MySqlCommand mycomm = new MySqlCommand(str, mycon);
            mycomm.CommandTimeout = 2;
            mycomm.ExecuteNonQuery();
            mycon.Close();
        }


        /// <summary>
        /// 编辑一个用户信息
        /// </summary>
        /// <param name="account">需要更改的帐号</param>
        /// <param name="type">需要更改的种类</param>
        /// <param name="value">需要更改的值</param>
        public static void EaditUser(string account, string type, string value)
        {
            mycon.Open();
            string str = "update  user set " + type + "='" + value + "' where Account='" + account + "' ";
            MySqlCommand mycom = new MySqlCommand(str, mycon);
            mycom.CommandTimeout = 2;
            mycom.ExecuteNonQuery();
            mycon.Close();
        }

        /// <summary>
        /// SQL用户登录请求处理
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pass"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Login(string account, string pass, out string message)
        {
            message = "";
            mycon.Open();
            string str = "select *from user where Account='" + account + "'";
            MySqlCommand mycomm = new MySqlCommand(str, mycon);
            mycomm.CommandTimeout = 2;
            MySqlDataReader Reader = mycomm.ExecuteReader();//读取并返回用户信息
            if (Reader.Read())
            {
                if (pass == (string)Reader.GetValue(3))
                {
                    message = "登录成功";
                    mycon.Close();
                    return true;
                    #region 服务器中查询用户是否在线
                    //if (RoomManager.OnlineUser.ContainsKey(account))
                    //{
                    //    ChatServer.LogInfo(account+"帐号已在线");

                    //    message = "你登录的帐号已在线!";
                    //    mycon.Close();
                    //    return false;
                    //}
                    //else
                    //{
                    //    message = "登录成功";
                    //    mycon.Close();
                    //    return true;
                    //}
                    #endregion
                }
                else
                {
                    message = "密码错误";
                    mycon.Close();
                    return false;
                }
            }
            else
            {
                message = "用户名不存在";
                mycon.Close();
                return false;
            }
        }

        /// <summary>
        /// 查询帐号是否被注册过
        /// </summary>
        /// <param name="account">查询的帐号</param>
        /// <param name="message">返回提示</param>
        /// <returns></returns>
        public static bool AccountOnly(string account, out string message)
        {
            message = "";
            mycon.Open();
            try
            {
                string str = "select *from user where Account='" + account + "'";
                MySqlCommand Myco = new MySqlCommand(str, mycon);
                MySqlDataReader read = Myco.ExecuteReader();
                if (read.Read())
                {
                    message = "帐号已被注册过";
                    mycon.Close();
                    return false;
                }
                else
                {
                    message = "";
                    mycon.Close();
                    return true;
                }

            }
            catch (System.Exception)
            {

                message = "服务器异常";
                return false;
            }
        }

        /// <summary>
        /// 读取一个用户的数据
        /// </summary>
        /// <param name="account">用户帐号</param>
        /// <returns>储存用户信息字典</returns>
        public static Dictionary<byte, object> loadUser(string account)
        {
            Dictionary<byte, object> dic = new Dictionary<byte, object>();
            mycon.Open();
            try
            {
                string str = "select *from user where Account='" + account + "'";
                MySqlCommand Myco = new MySqlCommand(str, mycon);
                MySqlDataReader read = Myco.ExecuteReader();
                if (read.Read())
                {

                    dic.Add(1, read.GetValue(1));   //用户昵称 
                    dic.Add(2, read.GetValue(2));    //用户帐号
                    //  dic.Add(3, read.GetValue(3));  //用户密码
                    dic.Add(4, read.GetValue(4));      //用户年龄
                    dic.Add(5, read.GetValue(5));      //用户性别
                    dic.Add(6, read.GetValue(6));      //用户金币
                    dic.Add(7, read.GetValue(7));      //用户等级
                    dic.Add(8, read.GetValue(8));      //用户电话
                }
            }
            catch (System.Exception)
            {
                dic.Add(1, null);
                dic.Add(2, null);
                dic.Add(3, null);
                dic.Add(4, null);

                throw;
            }
            mycon.Close();
            return dic;
        }

        /// <summary>
        /// 重置SQL参数
        /// </summary>
        /// <param name="password">管理员密码</param>
        /// <param name="tableName">表格名称</param>
        /// <param name="adminName">管理员帐号</param>
        /// <param name="IP">连接地址(本地不要填)</param>
        public static void SetSQL(string password = "123", string tableName = "userdata", string adminName = "root", string IP = "localhost")
        {
            string Path = "Server=" + IP + ";user id=" + adminName + ";password=" + password + ";database=" + tableName + "";
            mycon = new MySqlConnection(Path);
        }

        /// <summary>
        /// 为用户增加或者减少金币
        /// </summary>
        /// <param name="account">帐号</param>
        /// <param name="gold">增加或者减少的金币</param>
        public static void AddGold(string account, int gold)
        {
            //ChatServer.LogInfo("MysQ已打开进行数据更改");
            mycon.Open();
            int golds = 0;
            string str = "select *from user where Account='" + account + "'";
            MySqlCommand Mycom = new MySqlCommand(str, mycon);
            MySqlDataReader read = Mycom.ExecuteReader();
            if (read.Read())
            {
                golds = System.Convert.ToInt32(read.GetValue(6));
                golds += gold;

            }
            mycon.Close();

            mycon.Open();
            string str1 = "UPDATE `user` SET Gold=" + golds.ToString() + " WHERE Account='" + account + "' ";
            MySqlCommand mycom1 = new MySqlCommand(str1, mycon);
            mycom1.CommandTimeout = 2;
            mycom1.ExecuteNonQuery();

            mycon.Close();

            //ChatServer.LogInfo("MysQ数据更改完毕 并关闭");

        }
    }  

}

猜你喜欢

转载自blog.csdn.net/qq_39741605/article/details/80740225
今日推荐