当前位置:   article > 正文

Entity Framework Core执行存储过程_executesqlrawasync

executesqlrawasync

 

目录

1 存储过程

2 存储过程返回多个数据集

3 使用ExecuteSqlRawAsync()执行存储过程和原生SQL

4 使用ExecuteSqlRawAsync()输出存储过程参数


这节讲解EF Core中使用FromSqlRaw()& ExecuteCommand() 反复执行存储过程 

1 存储过程

下面存储过程返回指定 name和standard 所有学生,name&standard值通过存储过程的输入参数提供

  1. CREATE PROCEDURE [dbo].[sp_GetStudents]
  2. @Name VARCHAR(50),
  3. @Standard INT
  4. AS
  5. BEGIN
  6. SELECT * FROM Student Where Name=@Name AND Standard=@Standard
  7. END

Entity Framework Core 使用FromSqlRaw() 方法执行存储过,使用FromSqlRaw()方法时必须为存储过程提供两个参数,因此我们必须使用SqlParameter 数组设置参数

SqlParameter类位于Microsoft.Data.SqlClient命名空间内,下面代码执行存储过程​​​​​​​

 
  1. var param = new SqlParameter[] {
  2. new SqlParameter() {
  3. ParameterName = "@Name",
  4. SqlDbType = System.Data.SqlDbType.VarChar,
  5. Size = 100,
  6. Direction = System.Data.ParameterDirection.Input,
  7. Value = "Tony"
  8. },
  9. new SqlParameter() {
  10. ParameterName = "@Standard",
  11. SqlDbType = System.Data.SqlDbType.Int,
  12. Direction = System.Data.ParameterDirection.Input,
  13. Value = 10
  14. }};
  15. List<Student> studentList = context.Student.FromSqlRaw("[dbo].[sp_GetStudents] @Name, @Standard", param).ToList();

2 存储过程返回多个数据集

.FromSqlRaw()方法将从存储过程中获取一条记录,如果存储过程返回多个数据集,我们使用 ExecuteReader() 方法

下面存储过程返回两组数据集,一个是Student表,另一个是StudentAddress表​​​​​​​

  1. CREATE PROCEDURE [dbo].[sp_MultiRecordSets]
  2. @IdStu INT,
  3. @IdAdd INT
  4. AS
  5. BEGIN
  6. SELECT * FROM Student Where Id=@IdStu
  7. SELECT * FROM StudentAddress WHERE Id=@IdAdd
  8. END

下面代码显示了如何使用ExecuteReader() 方法从这两组数据中获取值​​​​​​​

  1. var param = new SqlParameter[] {
  2. new SqlParameter() {
  3. ParameterName = "@IdStu",
  4. SqlDbType = System.Data.SqlDbType.Int,
  5. Direction = System.Data.ParameterDirection.Input,
  6. Value = 5
  7. },
  8. new SqlParameter() {
  9. ParameterName = "@IdAdd",
  10. SqlDbType = System.Data.SqlDbType.Int,
  11. Direction = System.Data.ParameterDirection.Input,
  12. Value = 10
  13. }};
  14. var context = new SchoolContext();
  15. using (var cnn = context.Database.GetDbConnection())
  16. {
  17. var cmm = cnn.CreateCommand();
  18. cmm.CommandType = System.Data.CommandType.StoredProcedure;
  19. cmm.CommandText = "[dbo].[sp_MultiRecordSets]";
  20. cmm.Parameters.AddRange(param);
  21. cmm.Connection = cnn;
  22. cnn.Open();
  23. var reader = cmm.ExecuteReader();
  24. while (reader.Read())
  25. {
  26. // name from student table
  27. string studentName= Convert.ToString(reader["Name"]);
  28. }
  29. reader.NextResult(); //move the next record set
  30. while (reader.Read())
  31. {
  32. // city from student address table
  33. string city = Convert.ToString(reader["City"]);
  34. }
  35. }

注意使用NextResult()移动到下一组数据集

EF Core中三个最重要的概念:

1 DbContext类

2 Migrations

3 Fluent APIs

3 使用ExecuteSqlRawAsync()执行存储过程和原生SQL

EF Core使用ExecuteSqlRawAsync() 方法执行存储过程和原生SQL,但是不像FromSqlRaw()方法,它返回受影响的行数,注意也有一个ExecuteSqlRaw() 同步版本类型的方法

var rowsAffected = await context.Database.ExecuteSqlRawAsync("Update Students set Name = 'Donald Trump' where Id = 5");

4 使用ExecuteSqlRawAsync()输出存储过程参数

我们使用存储过程返回生总个数(@TotalStudents)​​​​​​​

  1. CREATE PROCEDURE [dbo].[sp_GetStudentsNew]
  2. @Name VARCHAR(50),
  3. @Standard INT,
  4. @TotalStudents INT OUTPUT
  5. AS
  6. BEGIN
  7. SELECT * FROM Student Where Name=@Name AND Standard=@Standard
  8. SET @TotalStudents= (SELECT COUNT(*) FROM Student)
  9. END

为了执行存储过程在EF Core,我们需要指定SqlParameter参数的输出方向​​​​​​​

  1. new SqlParameter() {
  2. ParameterName = "@TotalStudents",
  3. SqlDbType = System.Data.SqlDbType.Int,
  4. Direction = System.Data.ParameterDirection.Output, // direction output
  5. }

每次执行存储过程时,我们能通过输出参数获取到返回的值:

int totalStudents = Convert.ToInt32(Convert.ToString(param[2].Value));

注意:参数的开始索引是0,总共有三个参数,最后一个参数是输出参数​​​​​​​

  1. var context = new SchoolContext();
  2. var param = new SqlParameter[] {
  3. new SqlParameter() {
  4. ParameterName = "@Name",
  5. SqlDbType = System.Data.SqlDbType.VarChar,
  6. Size = 100,
  7. Direction = System.Data.ParameterDirection.Input,
  8. Value = "Tony"
  9. },
  10. new SqlParameter() {
  11. ParameterName = "@Standard",
  12. SqlDbType = System.Data.SqlDbType.Int,
  13. Direction = System.Data.ParameterDirection.Input,
  14. Value = 10
  15. },
  16. new SqlParameter() {
  17. ParameterName = "@TotalStudents",
  18. SqlDbType = System.Data.SqlDbType.Int,
  19. Direction = System.Data.ParameterDirection.Output,
  20. }};
  21. int affectedRows = await context.Database.ExecuteSqlRawAsync("[dbo].[sp_GetStudentsNew] @Name, @Standard, @TotalStudents out", param);
  22. int totalStudents = Convert.ToInt32(param[2].Value);

引用地址 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/253590
推荐阅读
相关标签
  

闽ICP备14008679号