更新SQL数据库条目

时间:2016-10-25 14:56:10

标签: c# mysql sql database visual-studio

我目前正在尝试为我的表单项目引入更新选项。我可以成功地将条目保存到数据库,但是如果用户将来获得需要在数据库中更新的信息,我希望能够更新这些条目......

目前我的错误是,“您的SQL语法中有错误;请查看与您的MYSQL服务器版本对应的手册,以便在''附近使用正确的语法'”lostID = 8,10月25日,10:00,10月25日,23:59,“,”“在第1行。

我认为这可能是设置语法,但如果删除它或更改任何内容,我会得到相同的错误。我不确定它是否因为我使用插入函数的参数,也许只是遗漏了什么?

我的代码是,

if (start_time.Text == end_time.Text)
        {
            MessageBox.Show("Please input a different ending time for the loss event");

        }
        else
        {
            string constring = "datasource=localhost;port=3306;username=root;password=1234; ";
            string query = "update lossdatabase.losstable set (lossID= '" + this.textBox1.Text + "', @Start, @Start_Time, @End, @End_Time, @Equipment, @Event, @Responsibility, @Cause, @Reason, Capacity_Loss='" + capacity + "', Planned= '" + Planned + "', Scheduled='" + Scheduled + "', Prepared='" + Prepared + "', @Primary_Rate, @Primary_Volume, @Primary_Percentage, @Secondary_Rate, @Secondary_Volume, @Secondary_Percentage, @Comment) where lossID= '" + this.textBox1.Text + "' ;";
            //Defines the connection string to allow data to be deposited into the database, along with defining the variables and columns for the data to be added to


            MySqlConnection conLossDB = new MySqlConnection(constring);
            MySqlCommand cmdLossDB = new MySqlCommand(query, conLossDB);


            cmdLossDB.Parameters.AddWithValue("@Start", start_date.Text);
            cmdLossDB.Parameters.AddWithValue("@Start_Time", start_time.Text);
            cmdLossDB.Parameters.AddWithValue("@End", end_date.Text);
            cmdLossDB.Parameters.AddWithValue("@End_Time", end_time.Text);
            cmdLossDB.Parameters.AddWithValue("@Equipment", comboBox1.Text);
            cmdLossDB.Parameters.AddWithValue("@Event", comboBox2.Text);
            cmdLossDB.Parameters.AddWithValue("@Responsibility", comboBox3.Text);
            cmdLossDB.Parameters.AddWithValue("@Cause", richTextBox1.Text);
            cmdLossDB.Parameters.AddWithValue("@Reason", richTextBox2.Text);
            cmdLossDB.Parameters.AddWithValue("@Primary_Rate", Rate_box.Text);
            cmdLossDB.Parameters.AddWithValue("@Primary_Volume", Volume_box.Text);
            cmdLossDB.Parameters.AddWithValue("@Primary_Percentage", Percentage_box.Text);
            cmdLossDB.Parameters.AddWithValue("@Secondary_Rate", Rate_2.Text);
            cmdLossDB.Parameters.AddWithValue("@Secondary_Volume", Volume_2.Text);
            cmdLossDB.Parameters.AddWithValue("@Secondary_Percentage", Percentage_2.Text);
            cmdLossDB.Parameters.AddWithValue("@Comment", richTextBox3.Text);

            //Defines which boxes to read in order to input the text from the defined boxes into the corresponding columns


            MySqlDataReader myReader;
            try
            {
                conLossDB.Open();
                myReader = cmdLossDB.ExecuteReader();
                MessageBox.Show("The loss entry has successfully been updated");
                //Opens the database and carries out the defined command outlined in the code above
                this.Close();
                while (myReader.Read())
                {

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

0 个答案:

没有答案