作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
尝试开发 Windows 8.1 商店应用程序。除其他困难外,我还需要在 where 子句上使用两个参数从 sqlite 数据库中检索记录。我可以在 where 子句中使用一个参数成功查询,但是当我尝试使用两个参数时它会崩溃。这是我的代码:
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;
}
请引用定义existingmaintenance
变量的行。此行的注释掉版本工作正常。当我用双参数查询替换变量定义时(使用不同的方法获得,因为我不知道如何向表查询方法添加第二个参数),它崩溃了。
感谢您提供的任何帮助。抱歉,我只理解了一半我在做什么。
最佳答案
假设您使用的是 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();
关于c# - 具有多个 WHERE 条件的 SQLite 数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24597888/
我是一名优秀的程序员,十分优秀!