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(); 读出数据然后再慢慢转换

SQLite timestamp field - converting to datetime - Stack Overflowicon-default.png?t=N7T8https://stackoverflow.com/questions/54914123/sqlite-timestamp-field-converting-to-datetime参考上文时间要加个978307200

double timestamp = 6220820844.00406+978307200;//这里时间要变换一下

            // 转换为 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 ;

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值