c# 读取sqlite3中的SQL type=TIMESTAMP字段时报错

复制代码
public static DateTime safeGetDateTime(SQLiteDataReader reader, string field)
        {
            int column = reader.GetOrdinal(field);
            if (!reader.IsDBNull(column))
            {
                return reader.GetDateTime(column);
            }

            return DateTime.Now;
        }
private void process(string dbPath)
        {
 
            int nNum = 0;
            SQLiteConnection connection = null;
            try
            {
                connection = new SQLiteConnection("Data Source=" + dbPath + ";version=3;new=False;datetimeformat=CurrentCulture;");
                connection.Open();
                SQLiteCommand command = new SQLiteCommand(connection);
                command.CommandText = "select    ZADDRESS,ZDURATION,ZORIGINATED, ZDATE ,Cast(ZDATE as nvarchar(20)) as ZDATE_NEW from  ZCALLRECORD ;";
                SQLiteDataReader reader = command.ExecuteReader(); 
             
                while (reader.Read())
                {
                    try
                    {
                        DateTime dateTime = SqliteDBTool.safeGetDateTime(reader, "ZDATE") ;
                        string  number = Encoding.UTF8.GetString(SqliteDBTool.safeGetBytes(reader, "ZADDRESS"));
                         
                        int dateTimeIndex=  reader.GetOrdinal("ZDATE_NEW") ; 
                        var date=reader["ZDATE_NEW"].ToString(); 
                        string duration = SqliteDBTool.safeGetFloat(reader, "ZDURATION").ToString("F1");

                    }
                    catch(Exception e)
                    {
                        add_log(e.ToString());
                    }
                }

                 
                reader.Close();
                connection.Close();
                connection.Dispose();
                 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (connection != null)
                {
                    connection.Close();
                    connection.Dispose();
                }
            }
        }

process(@"C:\CallHistory.storedata");

使用SQLite抛出异常: 该字符串未被识别为有效的 DateTime 错误。

参考如下

c# - sqlite throwing a "String not recognized as a valid datetime" - Stack Overflow

但我就想把数据读出来,就要使用Cast(ZDATE as nvarchar(20)) as ZDATE_NEW 把数据格式转一下,然后再用 var date=reader["ZDATE_NEW"].ToString(); 读出数据然后再慢慢转换

复制代码
double timestamp = 6220820844.00406;

            // 转换为 DateTime
            DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds((long)timestamp);
            DateTime dateTime = dateTimeOffset.UtcDateTime;
            add_log(dateTime.ToString("s"));

select ZADDRESS,ZDURATION,ZORIGINATED,ZDATE ,Cast(ZDATE as nvarchar(20)) as ZDATE_NEW from ZCALLRECORD ;

相关推荐
Scout-leaf2 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530142 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools3 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的4 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21884 天前
.NET 本地Db数据库-技术方案选型
windows·c#
lindexi4 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
百锦再4 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
tryCbest4 天前
数据库SQL学习
数据库·sql
qq_454245034 天前
基于组件与行为的树状节点系统
数据结构·c#
bugcome_com4 天前
C# 类的基础与进阶概念详解
c#