在asp.net MVC 5中从字符串转换日期时间时,转换失败

本文关键字:转换 日期 时间 失败 字符串 net asp MVC | 更新日期: 2023-09-27 18:14:14

我有一个项目使用asp.net mvc 5和数据库使用SQL Server 2008,当我在IIS服务器A中部署应用程序时使用windows 7其正常和工作,但当我在IIS服务器B中部署时使用windows Server 2008 sp 1,应用程序不工作。当我输入值时,错误是

    Server Error in '/SystemPurchase' Application.
Conversion failed when converting datetime from character string.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting datetime from character string.
Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace: 

[SqlException (0x80131904): Conversion failed when converting datetime from character string.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +388
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +688
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4403
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +82
   System.Data.SqlClient.SqlDataReader.get_MetaData() +135
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6665229
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +6667096
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +577
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +107
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +288
   System.Data.SqlClient.SqlCommand.ExecuteReader() +302
   SystemPurchase.Models.transaction.insert() in c:'Users'Administrator.USer-PC.000'Documents'Visual Studio 2013'Projects'SystemPurchase'SystemPurchase'Models'transaction.cs:196
   SystemPurchase.Controllers.NewEntryController.Index(HttpPostedFileBase filequo1, HttpPostedFileBase filetool1, HttpPostedFileBase filequo2, HttpPostedFileBase filetool2, HttpPostedFileBase filequo3, HttpPostedFileBase filetool3, HttpPostedFileBase filequo4, HttpPostedFileBase filetool4, HttpPostedFileBase filepo, HttpPostedFileBase filetool, FormCollection abc, transaction aa) in c:'Users'Administrator.USer-PC.000'Documents'Visual Studio 2013'Projects'SystemPurchase'SystemPurchase'Controllers'NewEntryController.cs:199
   lambda_method(Closure , ControllerBase , Object[] ) +835
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +217
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +12
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +139
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c() +112
   System.Web.Mvc.Async.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e() +452
   System.Web.Mvc.Async.<>c__DisplayClass30.<BeginInvokeActionMethodWithFilters>b__2f(IAsyncResult asyncResult) +15
   System.Web.Mvc.Async.<>c__DisplayClass28.<BeginInvokeAction>b__19() +32
   System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +231
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +51
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

我输入一个来自模型使用的日期

Datetime.Now.ToString("yyyy/MM/dd H:mm:ss tt")

在服务器A中工作,没有错误,但在服务器B中错误

有什么解决办法吗?

在asp.net MVC 5中从字符串转换日期时间时,转换失败

我建议您使用特定的FormatProvider,如下所示:

Datetime.Now.ToString("yyyy/MM/dd H:mm:ss tt", new CultureInfo("you culture Info"));

这里是一个有用的链接,您可以在其中创建一个包含所有可用区域性名称的列表。

您可以使用TryParseExact

选择你需要的格式

例如:

DateTime OutputDate = null;
 DateTime.TryParseExact(Datetime.Now.ToString(), "yyyy/MM/dd H:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out OutputDate);