EF中使用SQL語句或存儲過程 1、無參數查詢 var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToList(); 2、有參查詢 var model = db.Database.SqlQuery<UserI ...
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
p
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