我将数据插入到如下数据库中:
var N_ID = " 001T ";
var N_Email = N_EmailAdd;
TourCenterDBEntities NewsLetterEntities = new TourCenterDBEntities();
News_Letter NewsLetterDetails = new News_Letter();
NewsLetterDetails.N_id = N_ID;
NewsLetterDetails.N_Email = N_Email;
NewsLetterEntities.News_Letter.Add(NewsLetterDetails);
NewsLetterEntities.SaveChanges();
记录正确保存到数据库但在插入之前我想要该记录是否存在。我可以这样做。
答案 0 :(得分:3)
if(NewsLetterEntities.Any(entity => entity.N_id == N_ID)) {
// Return or throw, your choice
}
答案 1 :(得分:0)
我找到了办法。这对我有用。这就是它
$checkboxes = array(
'c_name1' => 0,
'c_name2' => 0,
);
$data = array();
foreach ($_POST as $param_name => $param_val) {
if($param_name != 'action' && $param_name != 'patient' && $param_name != 'jsaction') {
$name = sanitize($param_name);
$value = sanitize($param_val);
$data[$name] = $value;
}
}
$data = array_replace($checkboxes, $data);