【发布时间】:2016-07-20 09:17:26
【问题描述】:
我有在数据库中插入记录的功能。我想确保数据库中没有重复的条目。函数首先检查是否有查询字符串参数。如果有,那么它就像编辑模式,否则就像插入模式。有一个函数可以返回数据库中当前添加的记录。在插入数据库之前,我需要根据两列检查重复。
myService = new myService();
myFlow mf = new myFlow();
if (!string.IsNullOrEmpty(Request["myflowid"]))
{
mf = myService.Getmyflow(Convert.ToInt32(Request["myflowid"]));
}
int workcount = 0;
int.TryParse(txtWorkCount.Text, out workcount);
mf.Name = txtName.Text.Trim();
mf.Description = txtDescription.Text.Trim();
mf.FunctionCode = txtFunctioneCode.Text.Trim();
mf.FunctionType = txtFunctioneType.Text.Trim();
mf.WorkCount = workcount;
if (mf.WorkFlowId == 0)
{
mf.SortOrder = 0;
mf.Active = true;
mf.RecordDateTime = DateTime.Now;
message = "Saved Successfully";
}
else
{
_editMode = true;
message = "Update Successfully";
}
}
int myflowId = mfService.AddEditmyflow(mf);
我想检查基于functiontype 和functioncode 的重复。另一个函数mfService.Getmyflows()可以返回数据库中当前添加的记录。
如何使用 Linq 检查重复?
【问题讨论】:
-
我建议使用Entity framework(因为它更容易理解和数百个教程)来处理其中的数据库和数据。另外,我想说你可以在你的数据库中使用复合键,这会抛出 DuplicateKeyException,并且可能会被捕获。