Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PetaPoco/SQLite聚合日期错误的解决方法_Sqlite_Petapoco - Fatal编程技术网

PetaPoco/SQLite聚合日期错误的解决方法

PetaPoco/SQLite聚合日期错误的解决方法,sqlite,petapoco,Sqlite,Petapoco,下面是创建SQLite数据库的完整代码,将一些数据填充到表中,然后尝试检索它。如果datetime列周围有聚合函数,PetaPoco将抛出一个错误 using System; using PetaPoco; class Program { static void Main(string[] args) { bool filenew = false; if (!System.IO.File.Exists(@"c:\temp\database.sq3

下面是创建SQLite数据库的完整代码,将一些数据填充到表中,然后尝试检索它。如果datetime列周围有聚合函数,PetaPoco将抛出一个错误

using System;
using PetaPoco;

class Program
{
    static void Main(string[] args)
    {
        bool filenew = false;
        if (!System.IO.File.Exists(@"c:\temp\database.sq3"))
            filenew = true;
        System.Data.SQLite.SQLiteConnection sqltc = new System.Data.SQLite.SQLiteConnection("Data Source=" + @"c:\temp\database.sq3");
        sqltc.Open();
        PetaPoco.Database db = new Database(sqltc);
        if (filenew)
            db.Execute("create table test1 (ID_CHANNEL integer primary key autoincrement, dtfld DateTime null, name string)");
        test1 t = new test1();
        t.name = "No Date";
        db.Insert(t);
        t = new test1();
        t.dtfld = DateTime.Now;
        t.name = "with date";
        db.Insert(t);
// SUCCESS:
        test1 lt1 = db.First<test1>("select dtfld from test1 where ID_Channel = 2");
// FAILURE:
        test1 lt2 = db.First<test1>("select max(dtfld) as dtfld from test1 where dtfld is not null");
    }

    [PetaPoco.TableName("test1")]
    [PetaPoco.PrimaryKey("ID_Channel")]
    public class test1
    {
        public long ID_Channel { get; set; }
        public DateTime? dtfld { get; set; }
        public string name { get; set; }
    }
}
使用系统;
使用PetaPoco;
班级计划
{
静态void Main(字符串[]参数)
{
bool filenew=false;
如果(!System.IO.File.Exists(@“c:\temp\database.sq3”))
filenew=true;
System.Data.SQLite.SQLiteConnection sqltc=new System.Data.SQLite.SQLiteConnection(“数据源=“+@”c:\temp\database.sq3”);
sqltc.Open();
Database db=新数据库(sqltc);
如果(新文件)
Execute(“创建表test1(ID_通道整数主键自动递增,dtfld DateTime null,名称字符串)”);
test1t=newtest1();
t、 name=“无日期”;
db.插入(t);
t=新的test1();
t、 dtfld=DateTime.Now;
t、 name=“带日期”;
db.插入(t);
//成功:
test1 lt1=db.First(“从test1中选择dtfld,其中ID_Channel=2”);
//失败:
test1lt2=db.First(“从test1中选择max(dtfld)作为dtfld,其中dtfld不为null”);
}
[PetaPoco.TableName(“test1”)]
[PetaPoco.PrimaryKey(“ID_通道”)]
公共类test1
{
公共长ID_通道{get;set;}
公共日期时间?dtfld{get;set;}
公共字符串名称{get;set;}
}
}

有谁能建议一个仍然意味着POCO对象包含日期时间,并且我仍然可以访问最大日期的修复方案吗?

找到了一个解决方案-切换到NPoco。上面的唯一更改是将“PetaPoco”替换为“NPoco”。

在PetaPoco.cs中更改
私有静态函数GetConverter
函数。替换语句
return Convert.ChangeType(src,dstType,null)

TypeConverter conv = TypeDescriptor.GetConverter(dstType);
return conv.ConvertFrom(src);