c#, MySQL -在命令执行期间遇到致命错误-检查其他解决方案,我错过了一些东西

本文关键字:错过了 解决方案 其他 致命错误 命令 MySQL 执行期 遇到 检查 | 更新日期: 2023-09-27 18:09:37

我已经看了这个标题的其他问题,我认为问题是我缺失的代码的本地问题。

此按钮执行的功能是根据交易总额计算玩家获得的积分/奖励。例如,$10 =1分,$ 19=1分,$ 20=2。10点= 1奖励点,这等于10美元的信用。

我的代码收到标题错误消息。为了完整,我将包括整个函数。

    private void button1_Click(object sender, EventArgs e)
    {
        try{
            string cs = @"server=localhost;userid=root;password=root;database=dockingbay94";
            MySqlConnection conn;
            //MySqlDataReader rdr = null;
            using (conn = new MySqlConnection(cs));
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string input = textBox2.Text;
            MySqlCommand myCommand2 = conn.CreateCommand();
            myCommand2.CommandText = "SELECT Points FROM members WHERE id = @input";
            MySqlDataAdapter MyAdapter2 = new MySqlDataAdapter();
            MyAdapter2.SelectCommand = myCommand2;

            double transaction = Convert.ToDouble(textBox3.Text);
            double tmp_transaction = Math.Floor(transaction);
            string transaction_date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            double pointsbefore = (tmp_transaction / 10.0);
            int currentpoints = Convert.ToInt32(pointsbefore);
            int rewards = 0;
            int oldpoints = 0;
            string temp = "";
            pointsbefore = Math.Floor(pointsbefore);
            int new_points;
            double tmp_rewards = 0.0;
            double tmp_points;
            int new_rewards;
            oldpoints = (int)myCommand2.ExecuteScalar();
            new_points = currentpoints + oldpoints;
            tmp_points = new_points / 10;
            int tmp_rewards2 = 0;
            if (new_points > 10)
            {
                tmp_rewards = Math.Floor(tmp_points);
                tmp_rewards2 = Convert.ToInt32(tmp_rewards);
            }
            else if (new_points == 10)
            {
                tmp_rewards2 = 1;
            }
            else
            {
                tmp_rewards2 = 0;
            }
            new_rewards = rewards + tmp_rewards2;
            int points_left = 0;
            if (new_points > 10)
            {
                for (int i = 10; i < new_points; i++)
                {
                    points_left++;
                }
            }
            else if (new_points == 10)
            {
                points_left = 0;
            }
            else if (new_points < 10)
            {
                for (int i = 0; i < new_points; i++)
                {
                    points_left++;
                }
            }


        string query = "UPDATE members Set Points=@Points, rewards_collected=@Rewards, transaction_total=@Transaction, transaction_date=@TransactionDate" + "WHERE id = @input;";
        MySqlCommand cmdDataBase = new MySqlCommand(query, conn);
        cmdDataBase.Parameters.Add("@input", SqlDbType.Int).Value = Convert.ToInt32(textBox2.Text);
        cmdDataBase.Parameters.AddWithValue("@Points", new_points);
        cmdDataBase.Parameters.AddWithValue("@Rewards", new_rewards);
        cmdDataBase.Parameters.AddWithValue("@Transaction", textBox3.Text);
        cmdDataBase.Parameters.AddWithValue("@TransationDate", transaction_date);

        MySqlDataReader myReader2;
        myReader2 = cmdDataBase.ExecuteReader();
        MessageBox.Show("Data Updated");
    if(conn.State == ConnectionState.Open){
            conn.Close();
        }

    }
   catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

我不确定错误可能在哪里。可能没有发送正确的值。

谢谢

c#, MySQL -在命令执行期间遇到致命错误-检查其他解决方案,我错过了一些东西

这一行不对

using (conn = new MySqlConnection(cs));

去掉分号,在{}块中包含所有需要MySqlConnection变量的内容

using (MySqlConnection conn = new MySqlConnection(cs))
{
    // No need to test if the connection is not open....
    conn.Open();
    .........
    // Not needed (at least from your code above
    // MySqlDataAdapter MyAdapter2 = new MySqlDataAdapter();
    // MyAdapter2.SelectCommand = myCommand2;
    ... calcs follow here
    // Attention here, if the query returns null (no input match) this line will throw
    oldpoints = (int)myCommand2.ExecuteScalar();
    .... other calcs here

    MySqlCommand cmdDataBase = new MySqlCommand(query, conn);
    cmdDataBase.Parameters.Add("@input", SqlDbType.Int).Value = Convert.ToInt32(textBox2.Text);
    cmdDataBase.Parameters.AddWithValue("@Points", new_points);
    cmdDataBase.Parameters.AddWithValue("@Rewards", new_rewards);
    cmdDataBase.Parameters.AddWithValue("@Transaction", textBox3.Text);
    cmdDataBase.Parameters.AddWithValue("@TransationDate", transaction_date);
    // Use ExecuteNonQuery for INSERT/UPDATE/DELETE and other DDL calla
    cmdDataBase.ExecuteNonQuery();
    // Not needed
    // MySqlDataReader myReader2;
    // myReader2 = cmdDataBase.ExecuteReader();
    // Not needed, the using block will close and dispose the connection
    if(conn.State == ConnectionState.Open)
        conn.Close();
}

在最后的查询中还有另一个错误。@TransactionDate参数和WHERE子句之间缺少空格。在需要长SQL命令文本的情况下,我发现逐字字符串行字符连续@

非常有用。
string query = @"UPDATE members Set Points=@Points, rewards_collected=@Rewards, 
                        transaction_total=@Transaction, transaction_date=@TransactionDate
                        WHERE id = @input;";
相关文章: