当你有带日期的列时,我如何从 DataGridView 插入到数据库

How can i insert from DataGridView to database when you have Columns with Date

我正在尝试将数据从 DataGridView 输入到数据库中。我有 3 列包含日期。
当我尝试插入数据库时​​出现错误:

Conversion failed when converting date and/or time from character string

我用这个格式化显示日期的列:

private void Import()
{
    if (textBox4.Text.Trim() != string.Empty)
    {
        try
        {
            DataTable dt = GetDataTable(textBox4.Text);
            dataGridView2.DataSource = dt.DefaultView;
            dataGridView2.Columns[6].DefaultCellStyle.Format = "dd/mm/yyyy";
            dataGridView2.Columns[8].DefaultCellStyle.Format = "dd/mm/yyyy";
            dataGridView2.Columns[15].DefaultCellStyle.Format = "dd/mm/yyyy";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
}

对于这一部分,我从 DatagridView 中将数据输入到数据库中:

string constring = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
if (con.State == ConnectionState.Closed)
    con.Open();
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
    try
    {
        SqlCommand sqlCmd = new SqlCommand("insert into abonament (nr, serie, cui, client, sim, data_inst, activare, data_exp, telefon, nr_activat, nr_zile, ob, tip_client, email, datacurenta1, semnatura) values ('" + dataGridView1.Rows[i].Cells[0].Value + "','" + dataGridView1.Rows[i].Cells[1].Value + "','" + dataGridView1.Rows[i].Cells[2].Value + "','" + dataGridView1.Rows[i].Cells[3].Value + "','" + dataGridView1.Rows[i].Cells[4].Value + "','" + dataGridView1.Rows[i].Cells[5].Value + "','" + dataGridView1.Rows[i].Cells[6].Value + "','" + dataGridView1.Rows[i].Cells[7].Value + "','" + dataGridView1.Rows[i].Cells[8].Value + "','" + dataGridView1.Rows[i].Cells[9].Value + "','" + dataGridView1.Rows[i].Cells[10].Value + "','" + dataGridView1.Rows[i].Cells[11].Value + "','" + dataGridView1.Rows[i].Cells[12].Value + "','" + dataGridView1.Rows[i].Cells[13].Value + "','" + dataGridView1.Rows[i].Cells[14].Value + "','" + dataGridView1.Rows[i].Cells[15].Value + "')", con);
        sqlCmd.ExecuteNonQuery();

        MessageBox.Show("ok");
    }

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

这是我的解决方案

  for (int i = 0; i < dataGridView2.RowCount; i++)
        {
            DateTime d1 = Convert.ToDateTime(dataGridView2.Rows[i].Cells[6].Value.ToString());
            string format = "s";
            DateTime d2 = Convert.ToDateTime(dataGridView2.Rows[i].Cells[8].Value.ToString());
            DateTime d3 = Convert.ToDateTime(dataGridView2.Rows[i].Cells[15].Value.ToString());

            try
            {

                SqlCommand sqlCmd = new SqlCommand("insert into abonament (nr, serie, cui, client, sim, data_inst, activare, data_exp, telefon, nr_activat, nr_zile, ob, tip_client, email, datacurenta1, semnatura) values ( @nr, @serie, @cui, @client, @sim, @data_inst, @activare, @data_exp, @telefon, @nr_activat, @nr_zile, @ob, @tip_client, @email, @datacurenta1, @semnatura)", con);

                sqlCmd.Parameters.AddWithValue("@mode", "Add");
                sqlCmd.Parameters.AddWithValue("@id", 0);
                sqlCmd.Parameters.AddWithValue("@nr   ", dataGridView2.Rows[i].Cells[1].Value);
                sqlCmd.Parameters.AddWithValue("@serie   ", dataGridView2.Rows[i].Cells[2].Value);
                sqlCmd.Parameters.AddWithValue("@cui   ", dataGridView2.Rows[i].Cells[3].Value);
                sqlCmd.Parameters.AddWithValue("@client   ", dataGridView2.Rows[i].Cells[4].Value);
                sqlCmd.Parameters.AddWithValue("@sim   ", dataGridView2.Rows[i].Cells[5].Value);
                sqlCmd.Parameters.AddWithValue("@data_inst   ", d1.Date.ToString(format));
                sqlCmd.Parameters.AddWithValue("@activare   ", dataGridView2.Rows[i].Cells[7].Value);
                sqlCmd.Parameters.AddWithValue("@data_exp   ", d2);
                sqlCmd.Parameters.AddWithValue("@telefon   ", dataGridView2.Rows[i].Cells[9].Value);
                sqlCmd.Parameters.AddWithValue("@nr_activat   ", dataGridView2.Rows[i].Cells[10].Value);
                sqlCmd.Parameters.AddWithValue("@nr_zile   ", dataGridView2.Rows[i].Cells[11].Value);
                sqlCmd.Parameters.AddWithValue("@ob   ", dataGridView2.Rows[i].Cells[12].Value);
                sqlCmd.Parameters.AddWithValue("@tip_client   ", dataGridView2.Rows[i].Cells[13].Value);
                sqlCmd.Parameters.AddWithValue("@email   ", dataGridView2.Rows[i].Cells[14].Value);
                sqlCmd.Parameters.AddWithValue("@datacurenta1   ", d3);
                sqlCmd.Parameters.AddWithValue("@semnatura   ", dataGridView2.Rows[i].Cells[16].Value);

                sqlCmd.ExecuteNonQuery();

            }