数据库表未在c#中更新

时间:2016-06-20 00:25:32

标签: c# mysql database datagridview rows

我有以下代码在用户插入文本后从dataGridView更新数据库:

        private void button2_Click(object sender, EventArgs e)
    {
        string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";

        // Prepare a string where you insert parameter's placeholders instead of
        // concatenating the grid values....
        string Query = @"Update TopShineDB.table1 set CarColorNumber = @CarColorNumber, Interior = @Interior, 
                 Exterior = @Exterior , CPlastic = @CPlastic, MPlastic = @MPlastic, SPlastic = @SPlastic, 
                 PlasticB = @PlasticB, WashExt = @WashExt, WashEng = @WashEng, WashTrunk = @WashTrunk, 
                 WashSeats = @WashSeats, SeatsRmv = @SeatsRmv, SeatsFit = @SeatsFit, Notes = @Notes  
                 where `Time` = @Time";

        // Using statement around connection and command to destroy
        // these objects at the end of the using block               
        using (MySqlConnection conn = new MySqlConnection(constring))
        using (MySqlCommand command = new MySqlCommand(Query, conn))
        {
            conn.Open();

            // Create the list of parameters required by the query
            // Notice that you should use the appropriate MySqlDbType
            // for the field receiving the value.
            command.Parameters.Add("@Time", MySqlDbType.VarChar);
            command.Parameters.Add("@CarColorNumber", MySqlDbType.VarChar);
            command.Parameters.Add("@Interior", MySqlDbType.VarChar);
            command.Parameters.Add("@Exterior", MySqlDbType.VarChar);
            command.Parameters.Add("@CPlastic", MySqlDbType.VarChar);
            command.Parameters.Add("@MPlastic", MySqlDbType.VarChar);
            command.Parameters.Add("@SPlastic", MySqlDbType.VarChar);
            command.Parameters.Add("@PlasticB", MySqlDbType.VarChar);
            command.Parameters.Add("@WashExt", MySqlDbType.VarChar);
            command.Parameters.Add("@WashEng", MySqlDbType.VarChar);
            command.Parameters.Add("@WashTrunk", MySqlDbType.VarChar);
            command.Parameters.Add("@WashSeats", MySqlDbType.VarChar);
            command.Parameters.Add("@SeatsRmv", MySqlDbType.VarChar);
            command.Parameters.Add("@SeatsFit", MySqlDbType.VarChar);
            command.Parameters.Add("@Notes", MySqlDbType.VarChar);

    try
            {

                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    // Inside the loop update the parameters' values
                    // with data extracted by the current row...
                    command.Parameters["@Time"].Value = dr.Cells[0].Value;
                    command.Parameters["@CarColorNumber"].Value = dr.Cells[1].Value;
                    command.Parameters["@Interior"].Value = dr.Cells[2].Value;
                    command.Parameters["@Exterior"].Value = dr.Cells[3].Value;
                    command.Parameters["@CPlastic"].Value = dr.Cells[4].Value;
                    command.Parameters["@MPlastic"].Value = dr.Cells[5].Value;
                    command.Parameters["@SPlastic"].Value = dr.Cells[6].Value;
                    command.Parameters["@PlasticB"].Value = dr.Cells[7].Value;
                    command.Parameters["@WashExt"].Value = dr.Cells[8].Value;
                    command.Parameters["@WashEng"].Value = dr.Cells[9].Value;
                    command.Parameters["@WashTrunk"].Value = dr.Cells[10].Value;
                    command.Parameters["@WashSeats"].Value = dr.Cells[11].Value;
                    command.Parameters["@SeatsRmv"].Value = dr.Cells[12].Value;
                    command.Parameters["@SeatsFit"].Value = dr.Cells[13].Value;
                    command.Parameters["@Notes"].Value = dr.Cells[14].Value;

            // ExecuteNonQuery for INSERT/UPDATE/DELETE, 
            // ExecuteReader works but it is specific for reading
             command.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

我在应用程序中没有错误,一切运行正常,但当我检查表时,我意识到行中没有任何变化。为什么不更新?

0 个答案:

没有答案