繁体   English   中英

错误:字符串未被识别为有效的DateTime。

[英]Error: String was not recognized as a valid DateTime.

错误:字符串未被识别为有效的DateTime。 以下是堆栈跟踪-

“位于System.DateTimeParse.Parse(String s,DateTimeFormatInfo dtfi,DateTimeStyles样式)\\ r \\ n位于System.Convert.ToDateTime(字符串值)\\ r \\ n位于ConsoleApplication10.Program.b_1 1(<> f _AnonymousType0 1 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.WhereSelectListIterator 2 C:\\ Documents and Settings \\ hj81dev \\ My Documents \\ Visual Studio 2008 \\ Projects \\ ConsoleApplication10 \\ ConsoleApplication10 \\ Program.cs:line 28 \\中的ConsoleApplication10.Program.Main(String [] args)处的.MoveNext()\\ r \\ n r \\ n在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)\\ r \\ n在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)\\ r \\ n在Microsoft.VisualStudio.HostingProcess .HostProc.RunUsersAssembly()\\ r \\ n在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)\\ r \\ n在System.Threading.ExecutionContext.Run(ExecutionContext exe cutionContext,ContextCallback回调,对象状态)\\ r \\ n位于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;
            }
        }
    }

请只做那些需要的

设置cultureinfo以确保使用哪种格式

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM