sql 2012字符串未被识别为有效的日期时间 [英] sql 2012 string was not recognized as valid datetime

查看:85
本文介绍了sql 2012字符串未被识别为有效的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



这是我之前提出的类似问题,但未找到相同的解决方案。但正如我已经尝试并在代码中进行了一些更改,因此发布为新问题。



我使用文本框在asp.net页面中获取日期到sql 2012数据库。

代码在调试时工作正常但发布时出错。



数据库中的字段类型:datetime



用于转换的代码:

 Dim varTargetDate As Date = DateTime.ParseExact(Convert.ToDateTime(txtTargetDate.Text),dd- MM-yyyy,CultureInfo.InvariantCulture)





机器的短日期格式:

 dd-MM-yyyy 



机器的长日期格式:

 dd MMMM yyyy 



格式:英语(印度)



insert命令的参数:

 daCreate.InsertCommand.Parameters.Add(New SqlParameter(@ ROUTTD,varTargetDate))





我方需要的任何细节,请告诉我。



谢谢..





关注com在sql中检查存储格式:

 SELECT CONVERT(varchar(20),routtd,107)AS Expr1 FROM ROUT 



命令的结果:

 2014年10月9日

解决方案

你不应该使用

 DateTime.ParseExact(txtTargetDate.Text,  dd-MM- yyyy,CultureInfo.InvariantCulture)



而不是

 DateTime.ParseExact(转换。 ToDateTime(txtTargetDate.Text),  dd-MM-yyyy,CultureInfo.InvariantCulture)





您还有一个字符串中的日期,格式为MMM dd,yyyy,并使用dd-MM-yyyy来解析它???


除了解决方案1 ​​...



要更改SQL服务器上的日期格式,请参阅:< a href =http://technet.microsoft.com/en-us/library/aa259188%28v=sql.80%29.aspx> SET DATETIMEFORMAT [ ^ ]

Dear All,

This is a similar question I have asked earlier but have not found solution to the same. But as I have tried and made several changes in code, hence posting as new question.

I am using a textbox to get date in asp.net page to sql 2012 database.
The code works fine while debugging but gives error when published.

Field type in database: datetime

Code used for conversion:

Dim varTargetDate As Date = DateTime.ParseExact(Convert.ToDateTime(txtTargetDate.Text), "dd-MM-yyyy", CultureInfo.InvariantCulture)



Short date format of machine:

dd-MM-yyyy


long date format of machine:

dd MMMM yyyy


Format: English(India)

Parameter for insert command:

daCreate.InsertCommand.Parameters.Add(New SqlParameter("@ROUTTD", varTargetDate))



Any details required from my side, pls let me know.

Thanks ..


Following command in sql to check stored format:

SELECT CONVERT(varchar(20), routtd, 107) AS Expr1 FROM ROUT


The result from command:

Oct 09, 2014

解决方案

Shouldn't you use

DateTime.ParseExact(txtTargetDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture)


instead of

DateTime.ParseExact(Convert.ToDateTime(txtTargetDate.Text), "dd-MM-yyyy", CultureInfo.InvariantCulture)


?
Also you have a date in a string in the format "MMM dd, yyyy" and use "dd-MM-yyyy" to parse it???


In addition to solution 1...

To change date format on SQL server, please see: SET DATETIMEFORMAT[^]


这篇关于sql 2012字符串未被识别为有效的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆