private void button4_Click(object sender, EventArgs e)
        {
            MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='liblib';Data Source=localhost;username=root;password=admin");

            String query = "UPDATE loans SET dataRet=@data1 WHERE loans.idloans = @idloan";
            MySqlCommand cmd = new MySqlCommand(query, connection);

            int id = Int32.Parse(textBox9.Text);


                cmd.Parameters.Add("@data1", MySqlDbType.Date).Value = dateTimePicker1.Value;
                cmd.Parameters.Add("@idloan", MySqlDbType.Int32).Value = id;



                connection.Open();

                if (cmd.ExecuteNonQuery() == 1)
                {

                    MessageBox.Show("Succesful!");


                    connection.Close();

                    FIllCard();


                }


            else
            {

                MessageBox.Show("Error");
                connection.Close();
            }


当我在phpmyadmin中执行此UPDATE查询时,它将起作用并更新条目:


  更新贷款SET dataRet ='2017-5-6'WHERE loan.idloans = 23。


但是问题是当我在Form whith参数中尝试它时。它总是返回“错误”消息(ExecuteNonQuery与1不同),并且当我检查数据库时没有更新。我数据库中变量的类型为:
idloans-int; dataRet =日期;

最佳答案

查看此帖子:update a mySQL table using C#,它没有标记为解决方案的答案,但是使用第一个答案的代码后,该问题的OP出现身份验证问题,也许对您有用

10-07 17:38