方式1sql
//后台控制器验证
if (ModelState.IsValid == false)//后台控制器模型验证,false表示未通过
{
return Json("-1");
}
FHZMEntities fHZMEntities = new FHZMEntities();
//fHZMEntities.UserInfo.Add(userInfo);
//参数化列表
List<SqlParameter> sqlParameterList = new List<SqlParameter>() {
new SqlParameter("@UserName",userInfo.UserName),
new SqlParameter("@Number",userInfo.Number),
new SqlParameter("@UClass",userInfo.UClass),
new SqlParameter("@Ustate",userInfo.Ustate),
new SqlParameter("@Id",userInfo.Id),
};
int count = fHZMEntities.Database.ExecuteSqlCommand("insert UserInfo values(@UserName,@Number,@UClass,@Ustate)", sqlParameterList.ToArray());
return Json(count);
方式2直接add
FHZMEntities fHZMEntities = new FHZMEntities();
fHZMEntities.UserInfo.Add(userInfo);
int count = fHZMEntities.SaveChanges();
return Json(count);
方式3,标准流程,添加状态
FHZMEntities fHZMEntities = new FHZMEntities();
fHZMEntities.Entry(userInfo).State = System.Data.EntityState.Added;
int count = fHZMEntities.SaveChanges();
return Json(count);