2011-11-28 88 views
0

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

" at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)\r\n at System.Convert.ToDateTime(String value)\r\n at ConsoleApplication10.Program.b_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.MoveNext()\r\n at ConsoleApplication10.Program.Main(String[] args) in C:\Documents and Settings\hj81dev\My Documents\Visual Studio 2008\Projects\ConsoleApplication10\ConsoleApplication10\Program.cs:line 28\r\n at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadHelper.ThreadStart()"

这是我的代码。在SQL Server数据库中我的出生日期字段是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; 
      } 
     } 
    } 

请做要紧

+0

你想传递什么样的价值约会? – Oded

+1

你是否检查过Encoding.UTF8.GetString(a.x.DateOfBirth)实际返回的内容? –

+0

断点行并检查你正试图通过的值 – Purplegoldfish

回答

0

设置的CultureInfo可以肯定,使用哪种格式

var cultureInfo = new CultureInfo(yourCulture); 
Thread.CurrentThread.CurrentCulture = cultureInfo; 
相关问题