新增数据
作者:代富强 撰写日期:2020.8.7
新增数据:
三字码维护-新增三字码部分
根据页面提交的机场数据airport来进行一系列判断验证数据是否重复,无重复就新增数据到数据库,再拿到需要新增的数据实例并保存新增到数据库
S_Airport是机场表
{
ReturnJson msg = new ReturnJson();
if判断验证机场三字码
if (airport.airportCode != null && airport.airportCode.Length == 3 && Regex.IsMatch(airport.airportCode, “1{3}KaTeX parse error: Expected '}', got '&' at position 84: …t.airportName) &̲& Regex.IsMatch…”))// null “”
{
cityName是城市的名称
if (!string.IsNullOrEmpty(airport.cityName))
{
pinyinName是城市拼音
if (!string.IsNullOrEmpty(airport.pinyinName))
{
这里开始判断要新增的三字码或者要新增的机场名称是否已经存在
int oldAirportCount = (from tabAirport in myModel.S_Airport
where tabAirport.airportCode == airport.airportCode
|| tabAirport.airportName == airport.airportName
select tabAirport).Count();
没有重复的三字码和机场名称才新增,重复的给予提示修改
if (oldAirportCount==0)
{
新增数据到数据库
add需要新增的数据实例
myModel.S_Airport.Add(airport);
保存新增到数据库
if (myModel.SaveChanges() > 0)
{
msg.State = true;
msg.Text = “新增成功”;
} else {
msg.Text = “新增失败”;
}
} else {
msg.Text = “已经存在该三字码或者机场名称,不能添加”;
}
} else {
msg.Text = “城市拼音名称不能为空”;
}
} else {
msg.Text = “请填写城市名称”
}
} else {
msg.Text = “请填写机场名称”;
}
} else {
msg.Text = “三字码只能有3位”;
}
return Json(msg, JsonRequestBehavior.AllowGet);
}
如果需要新增的数据通过验证就会提示“新增成功”。
A-Z ↩︎