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 ;

相关推荐
RInk7oBjo7 小时前
spring-事务管理
数据库·sql·spring
View1213810 小时前
在 .NET 中使用 Moonshot Kimi + AgentFramework:从 SDK 到 Agent 的完整实践
c#·agent·kimi
FlDmr4i2811 小时前
.NET 10 & C# 14 New Features 新增功能介绍-扩展成员Extension Members
开发语言·c#·.net
splage11 小时前
Oracle分页sql
数据库·sql·oracle
QJtDK1R5a13 小时前
C# 14 中的新增功能
开发语言·c#
雨浓YN15 小时前
WebApi 通讯-DeepSeek API调用文档
c#
yuan1999716 小时前
C# 断点续传下载文件工具设计与实现
开发语言·c#
StarRocks_labs16 小时前
StarRocks I/O 模型揭秘(一):查询是如何被拆解与调度的?
starrocks·sql·pipeline·mpp·fe
cTz6FE7gA17 小时前
XSS、CSRF、SQL注入、防重放与敏感数据保护的分层策略
sql·xss·csrf
升职佳兴17 小时前
SQL 进阶4:查询从未下单的用户与 NOT EXISTS 完整解析
数据库·sql