EF执行SQL语句
程序员文章站
2024-02-20 16:52:24
...
使用EF框架的过程中,有时需要执行SQL语句来提升查询性能,如执行存储过程,进行复杂查询等。下面介绍如何在EF中执行SQL语句。
1.EF执行 SQL查询
string sql = "SELECT sStuID,sStuName FROM [dbo].[Students] WHERE sClassID = @classID";
var student = db.Database.SqlQuery<List<Students>>( sql, new SqlParameter("@classID ", classID));
var result = student.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
2.EF执行SQL操作
string sql = "DELETE FROM [dbo].[Students] WHERE sClassID = @ classID AND [email protected]";
SqlParameter[] args = new SqlParameter[2];
args[0] = new SqlParameter("@classID", classID);
args[1] = new SqlParameter("@stuID", stuID);
int result = db.Database.ExecuteSqlCommand(sql,args);