一、聚合函式查詢
double sum = 0; using (xxxEntities db = new xxxEntities()) { sum = db.userinfo.AsNoTracking().Where(d => d.id==id).Sum(m => (double?)m.money).GetValueOrDefault(); } return sum;
二、刪除操作
int result = 0; if (id > 0) { using (gghdbEntities db = new gghdbEntities()) { var data = db.quote.Where(d => d.id == id).FirstOrDefault(); db.quote.Remove(data); db.SaveChanges(); result = 1; } } return result;
三、執行事物(Net4.0以上版本支援)
using (xxxEntities db = new xxxEntities()) { using (TransactionScope transaction = new TransactionScope()) { //操作,出錯自動回滾 //提交事務 transaction.Complete(); } }
需要新增引用:using System.Transactions;