string 转datetime update入数据库

2025-06-22 14:40:40
推荐回答(3个)
回答1:

string str="
update tb set col=Convert(datetime,'" + dt + "') where id=1";
你可以这样手动进行转换。
希望可以帮到你,如果有什么问题的话,欢迎追问~
哦,对了,可能你的日期格式不正确。
如果你的是程序默认日期的话,是没有办法转换的。
(在SQL Server中的最小日期是1753-01-01)

回答2:

日期转换失败。。。。前台可以用JS来验证一下输入的是不是日期,后台获取之后的验证也就是
datetime dt;
if(DateTime.TryParse(str,out dt)==false)
{
Response.Write("您输入的日期格式不正确!");
Response.End();
}
string strSql="update tb set col='" + dt.ToString() + "' where id=1";
.....................

回答3:

col是dateTime
dt的值是什么?