插入和更新mysql值c#

时间:2017-01-11 09:25:59

标签: c# mysql mysql-connector

我在twitch bot中使用此代码来计算消息并将其每分钟保存到数据库中几次。

所以我当前的代码确实从我的数据库中获取了我想要的值,但我无法正确更新或插入值。 insert_cmd确实执行,但数据库中的值与我尝试插入的值不对应。 affectedRows确实返回应该受影响的行的正确答案。此外,当我写出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类如下所示:

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

数据库如下所示:database

此外,如果我在代码中做了其他愚蠢的事情,我很感激您是否可以提供一些提示:)

2 个答案:

答案 0 :(得分:1)

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

  

insert_cmd + = $“更新taperenmessages设置online_count ='{new_online_count}',online_count ='{new_offline_count}'WHERE id = '{读取器[ “ID”]}',“;

答案 1 :(得分:1)

您需要选择代码生成的查询。然后直接将其运行到Mysql中,然后比较它返回的内容。它看起来mysql返回受影响的最后执行查询的行。当您首先组合Update,然后组合Insert时,因此受影响的行将获得Insert。但您可以通过直接运行查询来确认它。一定要注释掉这样的代码:

// int affectedRows = myCommand.ExecuteNonQuery();