如何检查c#中是否存在记录

时间:2016-03-29 07:38:16

标签: c# sql database

我将数据插入到如下数据库中:

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();

记录正确保存到数据库但在插入之前我想要该记录是否存在。我可以这样做。

2 个答案:

答案 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);