Tho*_*mar 83
以下应该工作,是我的建议(参数化查询):
DateTime dateTimeVariable = //some DateTime value, e.g. DateTime.Now;
SqlCommand cmd = new SqlCommand("INSERT INTO <table> (<column>) VALUES (@value)", connection);
cmd.Parameters.AddWithValue("@value", dateTimeVariable);
cmd.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
Res*_*hma 18
DateTime time = DateTime.Now; // Use current time
string format = "yyyy-MM-dd HH:mm:ss"; // modify the format depending upon input required in the column in database
string insert = @" insert into Table(DateTime Column) values ('" + time.ToString(format) + "')";
Run Code Online (Sandbox Code Playgroud)
并执行查询.
DateTime.Now是插入当前日期时间..
小智 8
使用格式yyyy-mm-dd hh:mm:ss更为标准(IE:2009-06-23 19:30:20)
使用它你不必担心日期的格式(MM/DD/YYYY或DD/MM/YYYY).它将适用于所有这些.
atf*_*rgs -10
INSERT INTO <table> (<date_column>) VALUES ('1/1/2010 12:00')
Run Code Online (Sandbox Code Playgroud)