Execute Native SQL Query

You can execute native raw SQL query against the database using DBContext. You can execute the following types of queries:

  1. SQL query for entity types which returns particular types of entities
  2. SQL query for non-entity types which returns a primitive data type
  3. Raw SQL commands to the database

SQL query for entity types:

As we have seen in one of the previous chapters, DBSet has SQLQuery() method to write raw SQL queries which return entity instances. The returned objects will be tracked by the context, just as they would be if they were returned by a LINQ query. For example:

using (var ctx = new SchoolDBEntities())
{
var studentList = ctx.Students.SqlQuery("Select * from Student").ToList<Student>(); }

However, columns returned by SQL query should match the property of an entity type of DBSet otherwise, it will throw an exception. For example:

using (var ctx = new  SchoolDBEntities())
{
var studentName = ctx.Students.SqlQuery("Select studentid, studentname
from Student where studentname='New Student1'").ToList(); }

If you change the column name in query, then it will throw an exception because it must match column names:

using (var ctx = new SchoolDBEntities())
{
//this will throw an exception
var studentName = ctx.Students.SqlQuery("Select studentid as id, studentname as name
from Student where studentname='New Student1'").ToList();
}

SQL query for non-entity types:

A SQL query returning instances of any type, including primitive types, can be created using the SqlQuery method on the Database class. For example:

using (var ctx = new SchoolDBEntities())
{
//Get student name of string type
string studentName = ctx.Database.SqlQuery<string>("Select studentname
from Student where studentid=").FirstOrDefault<string>();
}

Raw SQL commands to the database:

ExecuteSqlCommnad method is useful in sending non-query commands to the database, such as the Insert, Update or Delete command. For example:

using (var ctx = new SchoolDBEntities())
{ //Update command
int noOfRowUpdated = ctx.Database.ExecuteSqlCommand("Update student
set studentname ='changed student by command' where studentid=");
//Insert command
int noOfRowInserted = ctx.Database.ExecuteSqlCommand("insert into student(studentname)
values('New Student')");
//Delete command
int noOfRowDeleted = ctx.Database.ExecuteSqlCommand("delete from student
where studentid="); }

最新文章

  1. mysql_connect() php7不支持,php5.5可以,是废弃函数
  2. TP框架,根据当前应用状态对应的配置文件
  3. Linux 指令
  4. [jQuery学习系列四 ]4-Jquery学习四-事件操作
  5. Mysql学习笔记(十一)临时表+视图
  6. placeholder在ie浏览器里不显示的问题解决
  7. ognl--数据运转的催化剂
  8. 通过LOGBACK实现每个类、包或自定义级别
  9. Elasticsearch 创建以及修改索引结构
  10. nginx比apache处理静态文件速度快,但是nginx处理大量并发的php请求时,容易出现502错误,频率大概是多少
  11. 磁盘操作系统 cmd命令
  12. 卓越研发之路 MOT技术管理者课堂
  13. 【AtCoder】AGC024
  14. linux下配置SS5(SOCK5)代理服务
  15. hdu2036(多边形面积)
  16. PPTP协议握手流程分析--转载
  17. linux 批量创建用户获取8位随机密码
  18. 关闭pm2
  19. Number 和 parseInt 区别
  20. 基于EasyNVR实现RTSP/Onvif监控摄像头Web无插件化直播监控

热门文章

  1. BEC listen and translation exercise 12
  2. skynet coroutine 运行笔记
  3. len(),range()函数
  4. centos type.h 编译错误问题
  5. [转载] 最简单的基于FFmpeg的AVDevice例子(读取摄像头)
  6. java-07接口与继承
  7. try catch(java)
  8. WPF开发中Designer和码农之间的合作
  9. 如何安装搜索引擎Elasticsearch?
  10. 如果有多个集合的迭代处理情况【使用MAP】