c#mysql在更新/添加/获取程序中的用户时延迟

b4lqfgs4  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(271)

我写的代码是保存用户从一个程序在sql表,但当15个用户在同一时间保存或更新我得到1分钟的延迟和程序没有响应。。。你能帮助我吗。这是我的密码。

public bool GetUser(ref clsConnection c)
    {
        try
        {
            MySqlConnection connect = new MySqlConnection(connectionMysql);
            connect.Open();
            MySqlCommand query = new MySqlCommand("SELECT * FROM Users WHERE User_Name='" + Escape(c.Username) + "'", connect);
            query.Prepare();
            MySqlDataReader dr = query.ExecuteReader();
            if (dr.Read())
            {
                c.Username = dr[1].ToString();
                c.NoColPlyName = dr[2].ToString();
                c.Cash = double.Parse(dr[3].ToString());
                c.Password = dr[4].ToString();
            }
            else
            {
                dr.Close();
                connect.Close();
                return false;
            }
            dr.Close();
            connect.Close();
            return true;
        }
    }
    public void UpdateUser(clsConnection u)
    {
        MySqlConnection cn = new MySqlConnection(connectionMysql);
        try
        {
            if (u.Username != "")
            {
                cn.Open();
                MySqlCommand query = new MySqlCommand(@"UPDATE Users SET User_Name=@User_Name,User_PlyName=@User_PlyName,User_Cash=@User_Cash,User_Passowrd=@User_Password WHERE User_Name='" + Escape(u.Username) + "';", cn);
                if (query != null)
                {
                    query.Parameters.AddWithValue("@User_Name", Escape(u.Username));
                    query.Parameters.AddWithValue("@User_PlyName", Escape(u.NoColPlyName));
                    query.Parameters.AddWithValue("@User_Cash", u.Cash);
                    query.Parameters.AddWithValue("@User_Passowrd", u.Password);
                    cn.Close();
                    return;
                }
                else
                {
                    return;
                }
            }
        }
    }
    public void AddUser(clsConnection c)
    {
        try
        {
            if (c.Username != "")
            {
                Query(@"INSERT INTO Users (User_Name,User_PlayerName,User_Cash,User_Passowrd) VALUES ('" +
                    Escape(c.Username) + "', '" +
                    Escape(c.NoColPlyName) + "', '" +
                    c.Cash + "', '" +
                    Espace(c.Passoword) + "');");
            }
        }
    }

//当15个用户尝试连接到程序时,程序没有响应,延迟非常大。当<10个用户连接到程序时,程序运行良好,但+10延迟较大。。。

u1ehiz5o

u1ehiz5o1#

您应该将查询放入如下using语句中:

using (MySqlConnection con = new MySqlConnection(connectionMysql))
{
   con.Open();

   using (MySqlCommand com = con.CreateCommand())
   {
      com.CommandText = "SELECT * FROM Users WHERE User_Name='" + Escape(c.Username) + "'";

      using (MySqlDataReader dr = com.ExecuteReader())
      {
         if (dr.Read())
         {
            c.Username = dr[1].ToString();
            c.NoColPlyName = dr[2].ToString();
            c.Cash = double.Parse(dr[3].ToString());
            c.Password = dr[4].ToString();
         }
         else
         {
            dr.Close();
            connect.Close();
            return false;
         }
         return true;
      }
   }
}

然后可以对更新和插入查询实现相同的方法

相关问题