SQL Web 服务的正确日期时间格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1109958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:44:47  来源:igfitidea点击:

Proper DateTime Format for a Web Service

sqlweb-servicesdate-formatformatexception

提问by user48408

I have a webservice with a method which is called via a xmlhttprequest object in my javascript. The method accepts a datetime parameter which is subsequently converted to a string and run against the database to perform a calculation.

我有一个 web 服务,它有一个通过 xmlhttprequest 对象在我的 javascript 中调用的方法。该方法接受一个日期时间参数,该参数随后被转换为字符串并针对数据库运行以执行计算。

I get the value from m_txtDateAdd and send off the xmlHttprequest

我从 m_txtDateAdd 获取值并发送 xmlHttprequest

<asp:textbox id=m_txtDateAdd tabIndex=4 runat="server" Width="96px" Text="<%# Today %>">
</asp:textbox>

which has a validator attacted to it

它有一个验证器

<asp:CustomValidator id="m_DateAddValidator" runat="server" ErrorMessage="Please Enter a Valid Date" ControlToValidate="m_txtDateAdd">&#x25CF;</asp:CustomValidator>

My webmethod looks something like this

我的网络方法看起来像这样

[WebMethod]
public decimal GetTotalCost(DateTime transactionDate)
{
    String sqlDateString = transactionDate.Year+"/"+transactionDate.Month+"/"+transactionDate.Day;

I use sqlDateString as part of the commandtext i send off to the database. Its a legacy application and its inline sql so I don't have the freedom to set up a stored procedure and create and assign parameters in my code behind. This works 90% of the time. The webservice is called on the onchange event of m_txtDateAdd. Every now and again the response i get from the server is

我使用 sqlDateString 作为我发送到数据库的命令文本的一部分。它是一个遗留应用程序和它的内联 sql,所以我没有自由设置存储过程并在我的代码中创建和分配参数。这在 90% 的情况下都有效。网络服务在 m_txtDateAdd 的 onchange 事件上被调用。我不时从服务器得到的响应是

System.ArgumentException: Cannot convert 25/06/2009 to System.DateTime. System.ArgumentException: Cannot convert 25/06/2009 to System.DateTime.

System.ArgumentException:无法将 25/06/2009 转换为 System.DateTime。System.ArgumentException:无法将 25/06/2009 转换为 System.DateTime。

Parameter name: type ---> System.FormatException: String was not recognized as a valid DateTime.

参数名称:类型 ---> System.FormatException:字符串未被识别为有效的 DateTime。

   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.DateTime.Parse(String s, IFormatProvider provider)
   at System.Convert.ToDateTime(String value, IFormatProvider provider)
   at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

回答by John Saunders

You need to send a DateTime in the correct format for XML: 2009-07-10T12:40Z. From http://en.wikipedia.org/wiki/ISO_8601.

您需要以正确的 XML 格式发送 DateTime:2009-07-10T12:40Z。来自http://en.wikipedia.org/wiki/ISO_8601



Also, why are you using HttpRequest? Why not just use Add Service Reference?

另外,你为什么使用HttpRequest?为什么不直接使用添加服务引用?

回答by Atish Narlawar

2001-10-26T19:32:52Z

2001-10-26T19:32:52Z

Use such format.

使用这样的格式。

回答by StuperUser

How are you creating the DateTime to pass into GetTotalCost()?

您如何创建 DateTime 以传递到 GetTotalCost()?

Have you got the correct constructor overload from http://msdn.microsoft.com/en-us/library/system.datetime.aspx?

你有没有从正确的构造函数重载 http://msdn.microsoft.com/en-us/library/system.datetime.aspx

回答by JGWeissman

Are you passing the String value from the text box directly to the webservice? It would be more reliable to parse the user input String into a javascript Date object, pass the Date object to the webservice, and let the serializer in Microsoft's ajax library figure out how to format it.

您是否将字符串值从文本框中直接传递到 Web 服务?将用户输入的 String 解析为 javascript Date 对象,将 Date 对象传递给 webservice,让微软的 ajax 库中的序列化器计算如何格式化会更可靠。

It may be helpful to see the client side javascript code that is getting the value and calling the web service.

查看获取值并调用 Web 服务的客户端 javascript 代码可能会有所帮助。