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