EF中使用SQL语句或存储过程
程序员文章站
2022-09-28 17:12:28
EF中使用SQL语句或存储过程 1、无参数查询 var model = db.Database.SqlQuery("select* from UserInfoes ").ToList(); 2、有参查询 var model = db.Database.SqlQuery
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
上一篇: c语言:在屏幕上输出图案
下一篇: C语言:求幂函数和指数函数的方法