C#Xamarin Android sqlite-pcl database.Query没有返回任何结果

时间:2016-08-02 14:24:40

标签: c# sqlite xamarin

我有一个函数返回一个名为EncounterList的类的IEnumerable列表。

如果我这样做它不起作用,则不会返回任何结果:

版本A:

public IEnumerable<EncounterList> GetEncounters4User4Date(long uid, string ApptDate)
        {
            string strSQL = "SELECT encounters.rotation as EncounterID, encounters.encounterdatetime as ApptTime, patients.surname || ', ' || patients.title || ' ' || patients.forename AS PatientName ";
            strSQL += "FROM encounters INNER JOIN patients ON encounters.patientid = patients.rotation LEFT OUTER JOIN consultlinkage on encounters.rotation = consultlinkage.encounterid ";
            strSQL += "WHERE encounters.userid=? AND consultlinkage.encounterid IS NULL AND strftime('%Y-%m-%d',encounters.encounterdatetime) = '?' ORDER BY encounters.encounterdatetime";
            var results= database.Query<EncounterList>(strSQL, uid, ApptDate);

            return results;
        }

如果我这样做,它确实有用。

版本B:

public IEnumerable<EncounterList> GetEncounters4User4Date(long uid, string ApptDate)
        {
            string strSQL = "SELECT encounters.rotation as EncounterID, encounters.encounterdatetime as ApptTime, patients.surname || ', ' || patients.title || ' ' || patients.forename AS PatientName ";
            strSQL += "FROM encounters INNER JOIN patients ON encounters.patientid = patients.rotation LEFT OUTER JOIN consultlinkage on encounters.rotation = consultlinkage.encounterid ";
            strSQL += string.Format("WHERE encounters.userid={0} AND consultlinkage.encounterid IS NULL AND strftime('%Y-%m-%d',encounters.encounterdatetime) = '{1}' ORDER BY encounters.encounterdatetime", uid, ApptDate);
            var results= database.Query<EncounterList>(strSQL);

            return results;
        }

有人可以向我解释一下我在例A中做错了什么吗?如果我使用string.format,我只能让它工作。

1 个答案:

答案 0 :(得分:0)

所以我终于发现了我做错了什么。我在调用Query Resolver时误解了参数的绑定方式。我假设您需要在版本A中的第二个参数周围使用单引号,并且解析器只是替换了?占位符与第二个参数的值,但不是这样。我发现如果我更改了以下行:

strSQL += "WHERE encounters.userid=? AND consultlinkage.encounterid IS NULL AND strftime('%Y-%m-%d',encounters.encounterdatetime) = '?' ORDER BY encounters.encounterdatetime";

....在版本A中:

strSQL += "WHERE encounters.userid=? AND consultlinkage.encounterid IS NULL AND strftime('%Y-%m-%d',encounters.encounterdatetime) = ? ORDER BY encounters.encounterdatetime";

IE删除参数周围的单引号,然后它就可以了。