【发布时间】:2017-09-13 03:11:36
【问题描述】:
我在尝试将字符串转换为 DateTime 时遇到问题。我尝试了一些方法,但在尝试更新记录时不断出现错误。
string consentGiven = ConsentGivenDate.Text;
DateTime date1 = Convert.ToDateTime(consentGiven);
cmd.Parameters.Add(new SqlParameter("@ConsentGivenDate", date1));
if ((ConsentGivenDate.Text == "dd/mm/yyyy"))
cmd.Parameters.Add("@ConsentGivenDate", SqlDbType.DateTime).Value = DBNull.Value;
else
cmd.Parameters.Add("@ConsentGivenDate", SqlDbType.DateTime).Value = ConsentGivenDate.Text;
我尝试添加一些格式,使日期为英国格式“dd/mm/yyyy”,但仍无法转换。
我做错了什么?
我是一个真正的编码新手,所以如果它是愚蠢的东西道歉......
【问题讨论】:
-
如果数据库中的列是
DateTime,为什么要插入字符串?只需cmd.Parameters.Add("@ConsentGivenDate", SqlDbType.DateTime).Value = date1
标签: c# asp.net string datetime