6

Trying to develop a Windows 8.1 Store App. Amongst other difficulties, I need to retrieve records from a sqlite database with two parameters on the where clause. I can successfully query with one parameter in the where clause but it crashes everything when I try to use two parameters. Here is my code for this:

    public string SaveMaint(MaintViewModel Maintenance)
    {
        string result = string.Empty;
        using (var db = new SQLite.SQLiteConnection(App.DBPath))
        {
            string change = string.Empty;
            try
            {
                var existingmaintenance = db.Query<maintenance> ("select * from maintenance where VehID = ? AND MaintID = ?", new String[] {Maintenance.Maintid, Maintenance.Vehicleid});
               // var existingmaintenance = (db.Table<maintenance>().Where 
               //     (c => c.MaintID == Maintenance.Maintid).SingleOrDefault());

                if (existingmaintenance != null)
                {
                    existingmaintenance.VehID = Maintenance.Vehicleid;
                    existingmaintenance.MaintID = Maintenance.Maintid; 
                    existingmaintenance.ServiceDate = Maintenance.Servicedate;
                    existingmaintenance.ServiceCost = Maintenance.Servicecost;
                    existingmaintenance.ServiceLocation = Maintenance.Servicelocation;
                    existingmaintenance.ServiceNote = Maintenance.Servicenote;
                    existingmaintenance.ServiceOdom = Maintenance.Serviceodom;
                    int success = db.Update(existingmaintenance);
                }
                else
                {
                    int success = db.Insert(new maintenance()
                    {
                        VehID = Maintenance.Vehicleid,
                        MaintID = Maintenance.Maintid,
                        ServiceDate = Maintenance.Servicedate,
                        ServiceCost = Maintenance.Servicecost,
                        ServiceLocation = Maintenance.Servicelocation,
                        ServiceNote = Maintenance.Servicenote,
                        ServiceOdom = Maintenance.Serviceodom
                    });
                }
                result = "Success";
            }
            catch
            {
                result = "This project was not saved.";
            }
        }
        return result;
    }

Please refer to the line in which existingmaintenance variable is defined. The commented out version of this line works fine. When I substitute the variable definition with the two parameter query (obtained using a different method because I couldn't figure out how to add a second parameter to the Table query approach), it crashes.

Thanks for any help you can give. Sorry that I only half understand what I'm doing.

4

2 回答 2

14

假设你使用SQLite-Net作为 ORM,你可以在查询之后传入参数。据我所知,不支持匿名类,如您的示例所示。尝试这个:

var existingmaintenance = db.Query<maintenance>(
    "select * from maintenance where VehID = ? AND MaintID = ?",
    Maintenance.Vehicleid, Maintenance.Maintid).FirstOrDefault();

您还可以使用 linq 查询,如下所示:

var existingmaintenance = db.Table<maintenance>().Where 
    (c => c.VehID == Maintenance.Vehicleid && 
    c.MaintID == Maintenance.Maintid).FirstOrDefault();
于 2014-07-06T17:05:28.833 回答
0

尝试这个

我希望两个 Id 在数据库中都是 int

var existingmaintenance = db.Query<maintenance> ("select * from maintenance where VehID = " + Maintenance.Maintid  + " AND MaintID = " + Maintenance.Vehicleid ).FirstOrDefault();
于 2014-07-06T16:29:53.483 回答