string str,a;
DateTime dat, aa;
using (con = new SqlConnection(@"Data Source=daniyal\sqlexpress;Initial Catalog=webassign;Integrated Security=True"))
{
con.Open();
using(com = new SqlCommand("select * from tasks",con))
{
rdr = com.ExecuteReader();
while (rdr.Read())
{
str = rdr.GetValue(1).ToString();
dat = DateTime.ParseExact(str, "dd/MM/yyyy", CultureInfo.InvariantCulture);
}
}
}
I want to parse a varchar
variable into datetime saved in my SQL Server
我想将varchar变量解析为保存在SQL Server中的datetime
1 个解决方案
#1
It doesn't work because the month is indicated with one digit but in the format string there are two MM. If you want to use ParseExact "the format of the string representation must match the specified format exactly." https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx
它不起作用,因为月份用一位数表示,但在格式字符串中有两位MM。如果你想使用ParseExact“字符串表示的格式必须与指定的格式完全匹配。” https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx
If you can't change the way you save the date in the database you need to find another strategy.
如果您无法更改在数据库中保存日期的方式,则需要找到另一种策略。
#1
It doesn't work because the month is indicated with one digit but in the format string there are two MM. If you want to use ParseExact "the format of the string representation must match the specified format exactly." https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx
它不起作用,因为月份用一位数表示,但在格式字符串中有两位MM。如果你想使用ParseExact“字符串表示的格式必须与指定的格式完全匹配。” https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx
If you can't change the way you save the date in the database you need to find another strategy.
如果您无法更改在数据库中保存日期的方式,则需要找到另一种策略。