asp.net MVC4 - 更新数据库,其中id =给出 [英] asp.net MVC4 - update database where id=given
本文介绍了asp.net MVC4 - 更新数据库,其中id =给出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么能在我的数据库更新行包括where子句?
我的C#codeS -
[HttpPost]
公众的ActionResult指数(串号,串音符)
{
SampleDBContext DB =新SampleDBContext(); //更新表名客户,集行`customerNote`与传递的字符串笔记,其中`id` ==定字符串ID
返回查看();
}
谁能帮我
解决方案
公共无效InsertOrUpdate(串号,串音符)
{
SampleDBContext的DbContext =新SampleDBContext();
VAR的客户=的DbContext .Customers.Where(E => e.ID = ID)
如果(customer.ID ==默认(的System.Guid)){
//新建实体
customer.UID = Guid.NewGuid();
的DbContext .customers.Add(客户);
}其他{
//现有实体
的DbContext .Entry(客户).STATE = System.Data.Entity.EntityState.Modified;
}
的DbContext .SaveChanges();
}
- 从表中获取数据行。
- 更新数据按需要
- 保存更改,需要将数据保存在表中。
How can I update a row in my database including where clause?
my c# codes-
[HttpPost]
public ActionResult Index(string ID, string notes)
{
SampleDBContext db = new SampleDBContext();
// update table name Customers, set row `customerNote` with passed string notes, where `id` == given string ID
return View();
}
Can anyone please help me
解决方案
public void InsertOrUpdate(string ID, string notes)
{
SampleDBContext dbContext = new SampleDBContext();
var customer= dbContext .Customers.Where(e=>e.ID =ID )
if (customer.ID == default(System.Guid)) {
// New entity
customer.UID = Guid.NewGuid();
dbContext .customers.Add(customer);
} else {
// Existing entity
dbContext .Entry(customer).State = System.Data.Entity.EntityState.Modified;
}
dbContext .SaveChanges();
}
- Get data row from table.
- Update data as per need
- Save changes are required to save the data in table.
这篇关于asp.net MVC4 - 更新数据库,其中id =给出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文