来自Datagridview的C#插入,无法找到列Visit_time

时间:2017-10-22 16:44:29

标签: c# mysql datagridview

您好我正在尝试从DataGridview插入数据现在它说无法找到Column visit_date,我在代码内部和里面看到它。

我有一个看起来像这样的表

visit_time,visit_date,fullname,visitor_address,visitor_city,visitor_telephone,id_method,organization,visit_type,reason,person_visit,img

我的代码看起来像这样

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    using (MySqlConnection cn = new MySqlConnection(constring))
    {
        cn.Open();
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            string sql = @"insert into final_appointments(visit_time,visit_date,fullname,visitor_address,visitor_city,visitor_telephone,id_method,organization,visit_type,reason,person_visit,img) values(@visit_time,@visit_date,@fullname,@visitor_address,@visitor_city,@visitor_telephone,@id_method,@organization,@visit_type,@reason,@person_visit,@img)";
            using (MySqlCommand cmd = new MySqlCommand(sql, cn))
            {
                try
                {
                    cmd.Parameters.AddWithValue("@visit_time", dataGridView1.Rows[i].Cells["visit_time"].Value);
                    cmd.Parameters.AddWithValue("@visit_date", dataGridView1.Rows[i].Cells["visit_date"].Value);
                    cmd.Parameters.AddWithValue("@fullname", dataGridView1.Rows[i].Cells["fullname"].Value);
                    cmd.Parameters.AddWithValue("@visitor_address", dataGridView1.Rows[i].Cells["visitor_address"].Value);
                    cmd.Parameters.AddWithValue("@visitor_city", dataGridView1.Rows[i].Cells["viitor_city"].Value);
                    cmd.Parameters.AddWithValue("@visitor_telephone", dataGridView1.Rows[i].Cells["visitor_telephone"].Value);
                    cmd.Parameters.AddWithValue("@id_method", dataGridView1.Rows[i].Cells["id_method"].Value);
                    cmd.Parameters.AddWithValue("@organization", dataGridView1.Rows[i].Cells["organization"].Value);
                    cmd.Parameters.AddWithValue("@visit_type", dataGridView1.Rows[i].Cells["visit_type"].Value);
                    cmd.Parameters.AddWithValue("@reason", dataGridView1.Rows[i].Cells["reason"].Value);
                    cmd.Parameters.AddWithValue("@person_visit", dataGridView1.Rows[i].Cells["person_visit"].Value);
                    cmd.Parameters.AddWithValue("@img", dataGridView1.Rows[i].Cells["img"].Value);

                    var msg = MessageBox.Show("You are About to Confirm Information"+Environment.NewLine+"Please Click 'OK' to Confirm?","Information",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                    if (msg == DialogResult.Yes)
                    {
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Appointment Fully Confirmed!", "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (msg == DialogResult.No)
                    {
                        return;
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}

我似乎不明白为什么它说无法找到列名,我在这里丢失

0 个答案:

没有答案