在我的應用程序中,我有一個dataGridview,它有一個從數據庫中獲取其數據的列,它表示一個類型爲date的列,當用戶單擊它時會有一個按鈕,它應該插入當前的選擇值另一個表中有一列類型爲日期的列。 我嘗試這樣做的那樣:從DataGridView插入到數據庫中的日期?
private void btnInesrtDate_Click(object sender, EventArgs e)
{
string date = dataGridView1.CurrentRow.Cells[2].ToString();
string sql = $"Insert Into BirthDates (Name, DateOfBirth)Values('{txtName.Text}','{date}')";
conn.Open();
SqlCommand command = new SqlCommand(sql, conn);
command.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Success");
}
,但我得到了一個錯誤說:從文字 字符串轉換日期和/或時間時
轉換失敗。
我得到了同樣的錯誤:( –
試試這個:'日期時間生日= Convert.ToDateTime(dataGridView1.CurrentRow.Cells [2] .value的);' –
好吧,它現在可以工作了我發現我在轉換爲字符串之前忘記了value屬性,但是我有一個問題,這是否意味着SQL中的日期類型只接受DateTime對象,並且不能接受字符串?我記得我以前做過! –