停止代码在查询中执行
本文关键字:执行 查询 代码 | 更新日期: 2023-09-27 18:01:44
我正在检查重复,它发现它们和If(isDuplicate)都工作正常。但是代码继续进行所有的提交,因为我没有让它停止。我该怎么做呢?停止并抛出它没有提交的错误面板?
_db.tbl_Localities.InsertOnSubmit(locality);
bool isDuplicate = _db.tbl_Localities
.Any(x => x.Locality == txt_Locality.Text);
if (isDuplicate)
{
pnl_Message.Visible = true;
lbl_message.Text = " Duplicate entry!";
txt_Locality.Text = "";
}
// Save
// ====
_db.SubmitChanges();
您应该处理else
部分的条件,或者在程序出现意外情况时引发异常:
_db.tbl_Localities.InsertOnSubmit(locality);
bool isDuplicate = _db.tbl_Localities
.Any(x => x.Locality == txt_Locality.Text);
if (isDuplicate)
{
pnl_Message.Visible = true;
lbl_message.Text = " Duplicate entry!";
txt_Locality.Text = "";
}
else
{
// Save
// ====
_db.SubmitChanges();
}
在if语句中使用return;