插入和更新 mysql 值 c#

Insert and Update mysql values c#

我在我的 twitch 机器人中使用此代码来计算消息数量并将它们每分钟保存到数据库中几次。

所以我当前的代码确实从我的数据库中获取了我想要的值,但我无法正确更新或插入这些值。 insert_cmd 确实执行了,但数据库中的值与我尝试插入的值不对应。 affectedRows 确实 return 应该受到影响的行的正确答案。另外,当我写出 insert_cmd 字符串时,它看起来确实正确。

     private static void update_messages()
    {
        try
        {
            MySql.Data.MySqlClient.MySqlConnection mysql_connection = new MySql.Data.MySqlClient.MySqlConnection();
            mysql_connection.ConnectionString = mysql_connection_string;
            mysql_connection.Open();

            //build query string
            string select_cmd = "SELECT * FROM taperen.messages where username in (";
            foreach(CountData cd in chat_messages)
            {
                //Console.WriteLine(cd.username);
                select_cmd += "\'" + cd.username + "\',";
            }
            if(select_cmd == "SELECT * FROM taperen.messages where username in (")
            {
                mysql_connection.Close();
                return;
            }
            select_cmd = select_cmd.TrimEnd(select_cmd[select_cmd.Length - 1]);
            select_cmd += ");";
            //Console.WriteLine(select_cmd);


            MySql.Data.MySqlClient.MySqlCommand myCommand = mysql_connection.CreateCommand();
            myCommand.CommandText = select_cmd;
            MySql.Data.MySqlClient.MySqlDataReader reader = myCommand.ExecuteReader();


            string insert_cmd = "";
            while (reader.Read())
            {
                string username = reader["username"].ToString();
                int index = chat_messages.FindIndex(x => x.username.Equals(username));

                int current_online_count = chat_messages[index].online_count;
                int current_offline_count = chat_messages[index].offline_count;

                int db_online_count = (int)reader["online_count"];
                int db_offline_count = (int)reader["offline_count"];

                int new_online_count = current_online_count + db_online_count;
                int new_offline_count = current_offline_count + db_offline_count;
                insert_cmd += $"UPDATE `taperen`.`messages` SET `online_count`='{new_online_count}', `online_count`='{new_offline_count}' WHERE `id`='{reader["id"]}';";

                chat_messages.RemoveAt(index);
                //Console.WriteLine(username);
            }
            reader.Close();
            mysql_connection.Close();
            foreach(CountData cd in chat_messages)
            {
                insert_cmd += $"INSERT INTO `taperen`.`messages` (`username`, `online_count`, `offline_count`) VALUES ('{cd.username}', '{cd.online_count}', '{cd.offline_count}');";
            }

            mysql_connection.Open();


            //Console.WriteLine(insert_cmd);

            myCommand.CommandText = insert_cmd;
            int affectedRows = myCommand.ExecuteNonQuery();
            Console.WriteLine(affectedRows);
            myCommand.Dispose();

            mysql_connection.Close();
        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

CountData class 如下所示:

public class CountData
{
    public string username { get; set; }
    public int online_count { get; set; }
    public int offline_count { get; set; }
}

数据库如下所示:

此外,如果我在我的代码中做了一些愚蠢的事情,如果你能给我一些提示,我将不胜感激:)

在这一行中,您设置了两次 online_count,第二个实例(大概)应该是 offline_count

insert_cmd += $"UPDATE taperen.messages SET online_count='{new_online_count}', online_count='{new_offline_count}' WHERE id='{reader["id"]}';";

您需要选择您的代码生成的查询。然后直接运行把它变成Mysql然后比较一下是什么returning。它看起来 mysql return 影响了上次执行查询的行。当您首先组合更新然后插入时,因此受影响的行正在插入。但是您可以通过直接 运行ning 您的查询来确认。请务必注释掉这样的代码:

// int affectedRows = myCommand.ExecuteNonQuery();