这是创建 SQLite 数据库、将一些数据填充到表中然后尝试检索它的完整代码。如果日期时间列周围有聚合函数,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; }
}
}
任何人都可以提出一个仍然意味着 POCO 对象包含日期时间的修复程序,并且我仍然可以访问日期的最大值吗?