字符串未被识别为有效的DateTime。在服务器2008 r2中部署 [英] String was not recognized as a valid DateTime. while deploy in server 2008 r2

查看:80
本文介绍了字符串未被识别为有效的DateTime。在服务器2008 r2中部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用网络应用程序。当我在本地系统中运行代码时它应该可以正常工作但如果我在服务器中托管我会收到错误

hi to all,
i'm using web application.when i run the code in local system it should work fine but if i host in server i got error "

String was not recognized as a valid DateTime.

对于datetime error.here我的项目也包括日期和时间。他登录时和退出时。所以如何解决它。我使用文化信息但是我仍然有错误。任何人都可以帮助我。这里我使用了windows server 2008 R2。



这里的一些我的日期字段数据类型为nvarchar.is它正确或想要更改为datetime

" for datetime error.here my project consists date and time also.whe he login and when he logout.so thing how to resolve it.here i used culture info also but still i got error.can anyone help me for this. here i used "windows server 2008 R2".

Another thing here some of my date fields datatype as nvarchar.is it correct or want to change as datetime

推荐答案

这种情况的根本原因通常是应用程序不使用参数。而是来自客户端的值直接连接到SQL语句。类似

Very often the root cause for this kind of situation is that the application does not use parameters. Instead values from client are concatenated directly to the SQL statement. Something like
... MyDateColumn = '" + datetimepicker123.value + "'...



如果你在客户端有这样的代码,你应该修改它以正确使用参数。



另一种情况是您使用参数但值是字符串并且完成了隐式转换。同样,最好的解决方案是使用正确类型的参数。如果目标列是datetime,那么参数的类型应该是datetime等等



最后一件事是,正如你所写,你已经存储了日期信息一个字符列。但是,您尝试将数据作为日期值处理。唯一可行的解​​决方案是将字符列转换为日期/时间列。如果存在本机数据类型,则没有理由以另一种格式存储数据。它只会造成不必要的麻烦...


If you have such code on the client side you should modify it to use parameters correctly.

Another scenario is that you use paramters but the value is a string and an implicit conversion is done. Again the best solution would be to use correct type of parameter. If the target column is datetime then the type of the parameter should be datetime and so on

And the last thing is, as you wrote, you have stored date information in a character column. However you try to handle the data as date values. The only feasible solution for this is to convert the character columns to date/time columns. There is no reason to store data in another format if a native data type is present. It just causes unnecessary troubles...


检查服务器的排序规则。

不同的排序规则会对日期和时间产生不同的影响。



示例 - http://www.sqlservercentral.com/Forums/Topic835597-146-1.aspx [ ^ ]。



有些网站建议使用 yyyymmdd hh:mm:ss 来存储日期和时间。
Check the server's collation.
Different collations can have different effects on date and time.

Example - http://www.sqlservercentral.com/Forums/Topic835597-146-1.aspx[^].

Some sites suggest using yyyymmdd hh:mm:ss to store dates and times.


不要将nvarchar数据类型用于date / datetime列。当您需要在sql server中存储日期时,请使用date或datetime数据类型。当你使用日期或日期时你最好使用sql参数并使用.net datetime对象设置参数值,你也可以将插入当前日期时间切换到数据库本身。例如,如果您需要插入具有当前日期时间的userid,那么您只需要从代码中插入asid作为参数,sql server将为您插入数据库当前日期时间。

don't use nvarchar datatype for date/datetime columns. use date or datetime data types when you need to store dates in sql server. when you working with date or datetimes you better use sql parameters and set the parameter values using .net datetime object and also you can handover the inserting current datetime to Database itself. for example if you need to insert userid with current datetime, then you only need to insert userid from as parameter from the code, sql server will insert database current datetime for you.
INSERT INTO UserLog(UserId, EventTime)
     VALUES (@userID, GETDATE())


这篇关于字符串未被识别为有效的DateTime。在服务器2008 r2中部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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