这节讲解EF Core中使用FromSqlRaw()& ExecuteCommand() 反复执行存储过程
1 存储过程
下面存储过程返回指定 name和standard 所有学生,name&standard值通过存储过程的输入参数提供
CREATE PROCEDURE [dbo].[sp_GetStudents]
@Name VARCHAR(50),
@Standard INT
AS
BEGIN
SELECT * FROM Student Where Name=@Name AND Standard=@Standard
END
Entity Framework Core 使用FromSqlRaw() 方法执行存储过,使用FromSqlRaw()方法时必须为存储过程提供两个参数,因此我们必须使用SqlParameter 数组设置参数
var param = new SqlParameter[] {
new SqlParameter() {
ParameterName = "@Name",
SqlDbType = System.Data.SqlDbType.VarChar,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = "Tony"
},
new SqlParameter() {
ParameterName = "@Standard",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Input,
Value = 10
}};
List<Student> studentList = context.Student.FromSqlRaw("[dbo].[sp_GetStudents] @Name, @Standard", param).ToList();
2 存储过程返回多个数据集
.FromSqlRaw()方法将从存储过程中获取一条记录,如果存储过程返回多个数据集,我们使用 ExecuteReader() 方法
下面存储过程返回两组数据集,一个是Student表,另一个是StudentAddress表
CREATE PROCEDURE [dbo].[sp_MultiRecordSets]
@IdStu INT,
@IdAdd INT
AS
BEGIN
SELECT * FROM Student Where Id=@IdStu
SELECT * FROM StudentAddress WHERE Id=@IdAdd
END
var param = new SqlParameter[] {
new SqlParameter() {
ParameterName = "@IdStu",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Input,
Value = 5
},
new SqlParameter() {
ParameterName = "@IdAdd",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Input,
Value = 10
}};
var context = new SchoolContext();
using (var cnn = context.Database.GetDbConnection())
{
var cmm = cnn.CreateCommand();
cmm.CommandType = System.Data.CommandType.StoredProcedure;
cmm.CommandText = "[dbo].[sp_MultiRecordSets]";
cmm.Parameters.AddRange(param);
cmm.Connection = cnn;
cnn.Open();
var reader = cmm.ExecuteReader();
while (reader.Read())
{
// name from student table
string studentName= Convert.ToString(reader["Name"]);
}
reader.NextResult(); //move the next record set
while (reader.Read())
{
// city from student address table
string city = Convert.ToString(reader["City"]);
}
}
EF Core中三个最重要的概念:
1 DbContext类
2 Migrations
3 Fluent APIs
3 使用ExecuteSqlRawAsync()执行存储过程和原生SQL
var rowsAffected = await context.Database.ExecuteSqlRawAsync("Update Students set Name = 'Donald Trump' where Id = 5");
4 使用ExecuteSqlRawAsync()输出存储过程参数
我们使用存储过程返回生总个数(@TotalStudents)
CREATE PROCEDURE [dbo].[sp_GetStudentsNew]
@Name VARCHAR(50),
@Standard INT,
@TotalStudents INT OUTPUT
AS
BEGIN
SELECT * FROM Student Where Name=@Name AND Standard=@Standard
SET @TotalStudents= (SELECT COUNT(*) FROM Student)
END
new SqlParameter() {
ParameterName = "@TotalStudents",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output, // direction output
}
int totalStudents = Convert.ToInt32(Convert.ToString(param[2].Value));
var context = new SchoolContext();
var param = new SqlParameter[] {
new SqlParameter() {
ParameterName = "@Name",
SqlDbType = System.Data.SqlDbType.VarChar,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = "Tony"
},
new SqlParameter() {
ParameterName = "@Standard",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Input,
Value = 10
},
new SqlParameter() {
ParameterName = "@TotalStudents",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output,
}};
int affectedRows = await context.Database.ExecuteSqlRawAsync("[dbo].[sp_GetStudentsNew] @Name, @Standard, @TotalStudents out", param);
int totalStudents = Convert.ToInt32(param[2].Value);
本文暂时没有评论,来添加一个吧(●'◡'●)