EF中使用SQL語句或儲存過程

hao_1234_1234發表於2018-04-10

EF中使用SQL語句或儲存過程

 
1、無引數查詢
var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToList();
 
2、有參查詢
var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes where id=@ID ",new SqlParameter("@ID",id)).ToList();
 
3、結合linq查詢
 
var model = (from in db.userinfo.SqlQuery("select * from UserInfoes where id=@ID"new SqlParameter("@ID", 1)) select p).ToList();
 
4:EF原始查詢單獨表
 
model = db.userinfo.OrderByDescending(a => a.ID).Skip(10 * (當前頁 - 1)).Take(10).ToList();
 
 
5,EF 執行儲存過程或delete 刪除語句
 
SqlParameter[] para = new SqlParameter[] {
new SqlParameter("@ID",id)
};
db.Database.ExecuteSqlCommand("delete UserInfoes  where id=@ID", para);
 
儲存過程 刪除,有幾個引數,儲存後面要帶幾個引數以逗號分隔
 
SqlParameter[] para = new SqlParameter[] {
new SqlParameter("@ID",id)
};
db.Database.ExecuteSqlCommand("sp_Userinfos_deleteByID @ID", para);
 
轉載來源:https://www.cnblogs.com/lvdongjie/p/5856306.html

相關文章