1

我正在尝试DataGridView使用以下代码更新我的数据库行:

private void button2_Click(object sender, EventArgs e)
 {
     foreach (GridViewRow dr in dataGridView1.Rows)
     {
         string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
         string Query = "Update TopShineDB.Table1 set Time = '" + dr.Cells[0].Text + "', CarColorNumber = '" + dr.Cells[1].Text + "', Interior = '" + dr.Cells[2].Text + "', Exterior = '" + dr.Cells[3].Text + "', CPlastic = '" + dr.Cells[4].Text + "', MPlastic = '" + dr.Cells[5].Text + "', SPlastic = '" + dr.Cells[6].Text + "', PlasticB = '" + dr.Cells[7].Text + "', WashExt = '" + dr.Cells[8].Text + "', WashEng = '" + dr.Cells[9].Text + "', WashTrunk = '" + dr.Cells[10].Text + "', WashSeats = '" + dr.Cells[11].Text + "', SeatsRmv = '" + dr.Cells[12].Text + "', SeatsFit = '" + dr.Cells[13].Text + "', Notes = '" + dr.Cells[14].Text + "', where Time = '" + dr.Cells[0].Text + "' ;";  
         MySqlConnection conn = new MySqlConnection(constring);
         MySqlCommand command = new MySqlCommand(Query, conn);
         MySqlDataReader myReader;

         try
         {
             conn.Open();
             myReader = command.ExecuteReader();
             MessageBox.Show("Table Successfully Updated");
             while (myReader.Read())
             {

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

但我最终得到了这个错误:

{"Unable to cast object of type 'System.Windows.Forms.DataGridViewRow' to type 'System.Web.UI.WebControls.GridViewRow'."}
4

2 回答 2

0

您正在执行 DataReader 并尝试更新。要更新您需要执行NonQuery。所以而不是:

myReader = command.ExecuteReader();

你需要

cmd.ExecuteNonQuery();

这是您在 foreach 循环下的代码需要的样子

string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
string Query = "Update TopShineDB.Table1 set Time = '" + dr.Cells[0].Text + "', CarColorNumber = '" + dr.Cells[1].Text + "', Interior = '" + dr.Cells[2].Text + "', Exterior = '" + dr.Cells[3].Text + "', CPlastic = '" + dr.Cells[4].Text + "', MPlastic = '" + dr.Cells[5].Text + "', SPlastic = '" + dr.Cells[6].Text + "', PlasticB = '" + dr.Cells[7].Text + "', WashExt = '" + dr.Cells[8].Text + "', WashEng = '" + dr.Cells[9].Text + "', WashTrunk = '" + dr.Cells[10].Text + "', WashSeats = '" + dr.Cells[11].Text + "', SeatsRmv = '" + dr.Cells[12].Text + "', SeatsFit = '" + dr.Cells[13].Text + "', Notes = '" + dr.Cells[14].Text + "', where Time = '" + dr.Cells[0].Text + "' ;";
MySqlConnection conn = new MySqlConnection(constring);
MySqlCommand command = new MySqlCommand(Query, conn);
                                    try
                        {
                            con.Open();
                            command.ExecuteNonQuery();
                            con.Close();
                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
于 2016-06-19T16:04:17.457 回答
0

它是 foreach(DataGridViewRow... 不是 foreach(GridViewRow... 如在msdn 文档中的.Rows 属性:

获取 DataGridViewRow 对象的数组。

为了使用此更改编译代码,您需要在访问单元格值时使用 .Value 而不是 .Text

于 2016-06-19T20:58:31.987 回答