【问题标题】:Error: String was not recognized as a valid DateTime.错误:字符串未被识别为有效的日期时间。
【发布时间】:2012-01-07 20:20:33
【问题描述】:

错误:字符串未被识别为有效的日期时间。 下面是堆栈跟踪-

" 在 System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles 样式)\r\n 在 System.Convert.ToDateTime(String 值)\r\n 在 ConsoleApplication10.Program.b_1(f_AnonymousType01 a) in C:\\Documents and Settings\\xxxxdev\\My Documents\\Visual Studio 2008\\Projects\\ConsoleApplication10\\ConsoleApplication10\\Program.cs:line 23\r\n at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext()\r\n at C:\Documents 中的 ConsoleApplication10.Program.Main(String[] args) 和 设置\hj81dev\我的文档\Visual Studio 2008\Projects\ConsoleApplication10\ConsoleApplication10\Program.cs:line 28\r\n 在 System.AppDomain._nExecuteAssembly(程序集程序集, String[] args)\r\n 在 System.AppDomain.ExecuteAssembly(String assemblyFile, 证据 assemblySecurity, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)\r\n 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)\r\n at System.Threading.ThreadHelper.ThreadStart()"

这是我的代码。在 sql server 数据库中,我的 dateofbirth 字段是 varbinary 类型

class Program
    {
        static void Main(string[] args)
        {
            var customerProfileGuid = new Guid("35D02589-C5FA-437D-B661-000215C68584");
            using (CustomerProfileEntities context = new CustomerProfileEntities())
            {
                var test = from x in context.CustomerProfile
                           where x.CustomerProfileId == customerProfileGuid
                           select new { x };

                var customerData = test.ToList();
                var customerResult = (from a in customerData
                                      select new Profile
                                      {
                                          DateOfBirth =Convert.ToDateTime(Encoding.UTF8.GetString(a.x.DateOfBirth)) //getting error here
                                      });

                foreach (var profile in customerResult)
                {
                    var profileData = profile;
                }
            }

        }
    }
    public class Profile
    {
        private DateTime dateOfBirthField;

        [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
        public DateTime DateOfBirth
        {
            get
            {
                return this.dateOfBirthField;
            }
            set
            {
                this.dateOfBirthField = value;
            }
        }
    }

请做必要的事情

【问题讨论】:

  • 你想为约会传递什么值?
  • 您检查过 Encoding.UTF8.GetString(a.x.DateOfBirth) 实际返回的内容吗?
  • 断点该行并检查您尝试传入的值
  • 得到的值为byte[] = 0x00009F960121136F

标签: c# linq-to-entities


【解决方案1】:

设置cultureinfo确定,使用哪种格式

var cultureInfo = new CultureInfo(yourCulture);
Thread.CurrentThread.CurrentCulture = cultureInfo;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2012-04-24
    相关资源
    最近更新 更多