【发布时间】:2016-03-20 16:42:12
【问题描述】:
我已将 SQLite 数据库附加到我的 Windows Phone 8.1 项目中。连接正常,但从数据库返回的结果为空。
之前我发布了我的查询的question on the null bindings。但是建议的答案并没有解决返回空结果的问题,这就是我重新发布问题的原因。
调试:单步执行 SQLlite 类,我可以看到绑定为空,尽管考虑到我的 DB 架构中的类型和ZoneInfo.cs 中的关联字段映射。
我按如下方式查询数据库,该数据库应将每个字段映射到下面我的类 ZoneInfo。
using (var dbConn = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, AppDBPath), true))
{
List<ZoneInfo> zoneInfo = dbConn.Query<ZoneInfo>("select * from " + tableName).ToList<ZoneInfo>();
ObservableCollection<ZoneInfo> zoneInfoCollection = new ObservableCollection<ZoneInfo>(zoneInfo);
return zoneInfoCollection;
}
问题:
如何更正我的数据库映射以使查询结果不为空?
**ZoneInfo.cs (mapping class for DB fields)**
public class ZoneInfo
{
//The ObjectId property is marked as the Primary Key
[SQLite.PrimaryKey]
[Column("objectId")]
public string ObjectId { get; set; }
[Column("zone")]
public string ZoneName { get; set; }
[Column("tariff_ph")]
public float TariffPH { get; set; }
[Column("tariff_pd")]
public float TariffPD { get; set; }
[Column("restrictions")]
public string Restrictions { get; set; }
[Column("days_of_operation")]
public string DaysOpen { get; set; }
[Column("hours_of_operation")]
public string HoursOpen { get; set; }
public ZoneInfo()
{
}
public ZoneInfo(string objectId, string zoneName, int tariffPH, int tariffPD,
string restrictions, string daysOpen, string hoursOpen )
{
ObjectId = objectId;
ZoneName = zoneName;
TariffPH = tariffPH;
TariffPD = tariffPD;
Restrictions = restrictions;
DaysOpen = daysOpen;
HoursOpen = hoursOpen;
}
}
查询结果:(您可以看到查询字段的结果都是空的,这在数据库中不是这样的:
DB 架构如下,这是一个链接到DB file 用于测试:
【问题讨论】:
-
您的上一个 Question 包含一个示例 repo。在执行它时发现你的
ReadZones读取并绑定了正确的值。那么您确定它的绑定架构错误不是您可能进行的其他更改。 -
@Jerin DBHelper 代码是相同的,因为我发布了那个问题..所以不确定可能有什么区别。我目前的仓库在这里github.com/BrianJVarley/Parking-Tag-Sms-Sender/blob/master/…
-
测试了您的存储库我能够在
TagRequestViewModel类中接收您的数据库的值,InitZoneInfoAsync()方法返回行var zoneResult = _dbHelper.ReadZones(tableName);的值所以您的ObservableCollection<ZoneInfo> ReadZones被调用并且工作正常。你能指定你在哪里得到空值吗? -
@Jerin 我在 DbHelper 类的 ReadZone() 上设置了一个断点。当我检查返回的 zoneInfoCollection 中每个 ZoneInfo 项的值时,字段值为空。您能否发布您在 TagRequestViewModel 中看到的返回值的屏幕截图?
-
这是Image link 如您所见,我返回了 5 个值。我刚刚下载了你的 repo 并运行它并没有改变任何东西。
标签: c# database data-binding sqlite windows-phone-8.1