C# sqlite 无法识别的datetime格式 FromOADate

再读取不可控数据库datetime字段时,遇到了一个奇葩问题——“无法识别的datetime格式”

搞了半天……

数据库里看:2017-06-06 10:28:30.000

不做处理查询报错:“无法识别的datetime格式”

做处理后查出来个:42892.4364583333

……

接下来这么处理

DateTime dtr = DateTime.FromOADate(42863.5656134259);
                double ewr = dtr.ToOADate();
                DateTime val = new DateTime(1899, 12, 31);
                val = val.AddDays(42863.5656134259);
SQLiteConnection connection= new SQLiteConnection(string.Format(
                            "Data Source={0}skpdb_539900210037.db3;Version=3;",
                            AppDomain.CurrentDomain.SetupInformation.ApplicationBase));//              
                connection.Open();
                DataTable dt = new DataTable();
                //connection.Open();
                SQLiteCommand command = new SQLiteCommand(connection);
                command.CommandText = "select CAST(kprq AS double) from zzs_fpkj order by kprq";
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
                adapter.Fill(dt); 
                foreach (DataRow dr in dt.Rows)
                {
                    string aaa = DateTime.FromOADate(Convert.ToDouble(dr[0])).ToString();
                    Console.WriteLine(aaa);
                }
                connection.Close();

  

原文地址:https://www.cnblogs.com/dyfisgod/p/7477512.html