IM具有problam插入一個字符串,它看起來像一個日期(2015年2月23日) 從DataGridView到我的本地數據庫日期列。插入一個字符串,它看起來像一個日期到數據庫日期列
我知道我需要將我的字符串「23.02.2015」轉換爲23/02/2015,並在將它插入到我的數據庫日期列之前將其轉換爲日期變量,但是我不知道如何在我的代碼中執行該操作:
private void button3_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string constring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\john\Documents\Visual Studio 2015\Projects\Project\Project\DB.mdf;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO ResultsTable VALUES(@Date, @TagNumber)", con))
{
cmd.Parameters.AddWithValue("@Date", row.Cells["Exposure Date"].Value);
cmd.Parameters.AddWithValue("@TagNumber", row.Cells["Device #"].Value);
cmd.ExecuteNonQuery();
}
}
}
MessageBox.Show("Records inserted.");
}
總之
- 有problam IM將字符串轉換,如「2014年5月23日」的日期類型像23/05/2014,將其插入到日期列在我的數據庫在我的代碼。
你要像'Convert.ToDateTime(Regex.Replace(輸入 「」, 「/」))'自動轉換爲字符串日期時間插入之前? –
是的!但在我的代碼中插入這條線以適應? – shlezz