-2
在使用数据库的连接方法中,如何从日期时间选择器的窗体窗体中添加日期字符串?ADO.NET连接方法
在使用数据库的连接方法中,如何从日期时间选择器的窗体窗体中添加日期字符串?ADO.NET连接方法
请务必使用属性类型,因此请勿使用string
作为datetime
。您应该使用参数来防止sql注入:
using (var con = new SqlConnection(connectionString))
{
using (var cmd = new SqlCommand("INSERT INTO dbo.Table(DateCol)VALUES(@Date)", con))
{
cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = dateTimePicker1.Value;
con.Open();
cmd.ExecuteNonQuery();
}
}