【问题标题】:Webservice - DateTime Error网络服务 - 日期时间错误
【发布时间】:2018-04-17 21:39:59
【问题描述】:

我正在尝试检查我的网络服务是否运行良好。所有其他的都可以,但是每当我运行这个特定的时,我都会出错。在我的数据库中,DateOfBirth 的数据类型是“日期时间”,DateApplied 的数据类型也是“日期时间”。但是每当我想在浏览器上检查它是否正常工作时,我都会收到以下错误消息:

System.FormatException: The string was not recognized as a valid DateTime.
There is an unknown word starting at index 0.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.Convert.ToDateTime(String value)
   at FinalService.getfinal() in c:\Users\Daniel\Documents\Visual Studio 2012\WebSites\DevinApp\App_Code\FinalService.cs:line 26

下面是我的代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Services;
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
public class FinalService : System.Web.Services.WebService 
    {
        [WebMethod]
        public void getfinal()
        {
            var cs = ConfigurationManager.ConnectionStrings["NNPC-ChevronScholarshipConnectionString"].ConnectionString;
            var students = new List<Final>();
            using (var con = new SqlConnection(cs))
            {
                var cmd = new SqlCommand("getapplicants", con) { CommandType = CommandType.StoredProcedure };
                con.Open();
                var dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var student = new Final
                    {
                        ID = Convert.ToInt32(dr[0].ToString()),
                        ApplicationID = dr[1].ToString(),
                        FirstName = dr[2].ToString(),
                        MiddleName = dr[3].ToString(),
                        LastName = dr[4].ToString(),
                        DateOfBirth = Convert.ToDateTime(dr[5].ToString()),

                        Gender = dr[6].ToString(),
                        Phone = dr[7].ToString(),
                        Email = dr[8].ToString(),
                        DateApplied = Convert.ToDateTime(dr[9].ToString()),
                        CurrentLevel = dr[10].ToString(),
                        MatricNo = dr[11].ToString()

                    };
                    students.Add(student);
                }
            }
            var js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(students));
        }

}

【问题讨论】:

  • dr[5].ToString() 的值是多少?数据库中该列的数据类型是什么? dr[9] 也一样。
  • 数据类型为日期时间
  • 我问了四个问题,你回答了一个。你能回答所有四个吗?
  • 请告诉我们getapplicants存储过程。
  • 没有值 dr[5] 指向我数据库中的列

标签: c# sql asp.net datetime type-conversion


【解决方案1】:

这意味着dr[5].ToString() 返回的不是有效的DateTime。它遇到“索引 0 处的未知单词”——这意味着字符串的第一个字符对 DateTimes 无效。

我会在数据库的该列中查找错误数据。

编辑:

另外,不必转换为字符串。您可以直接从 SQL DateTime 转换为 C# DateTime。与此非常相似的东西应该可以工作:

 dr.GetDateTime(5);

【讨论】:

  • 我试过 dr.GetDateTime(5);我收到此错误:System.InvalidCastException: Specified cast is not valid。在 System.Data.SqlClient.SqlBuffer.get_DateTime() 在 System.Data.SqlClient.SqlDataReader.GetDateTime(Int32 i)
  • 这可能意味着您的数据库中有错误的数据。我将运行 SQL 查询以从所有行中选择该列并查看结果。如果其中一些看起来不像 DateTimes,那就是问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多