所以这个link,显示了如何传递一个int数组到存储过程。什么是等效传递数组int到存储过程.net 4.6代码.net核心?
CREATE TYPE dbo.EmployeeList
AS TABLE
(
EmployeeID INT
);
GO
CREATE PROCEDURE dbo.DoSomethingWithEmployees
@List AS dbo.EmployeeList READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT EmployeeID FROM @List;
END
GO
和C#代码:
DataTable tvp = new DataTable();
// define/populate DataTable from your List here
using (conn)
{
SqlCommand cmd = new SqlCommand("dbo.DoSomethingWithEmployees", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvparam = cmd.Parameters.AddWithValue("@List", tvp);
tvparam.SqlDbType = SqlDbType.Structured;
// execute query, consume results, etc. here
}
但由于.NET的核心不支持数据表,这将是对.NET核心的等效代码?特别是在AddWithValue方法中添加tvp变量作为参数,并且最后一行代码是tvparam.SqlDbType = SqlDbType.Structured?
编辑
所以我有这个存储过程有三个参数。 @StudentIds是一个int数组。
ALTER PROCEDURE stp_Student
@Name nvarchar(450),
@StudentIds tvp_ArrayInt READONLY,
@StartDate date
AS
blah blah
//returns student summary
SELECT stu.StudentId,
stu.StudentName,
CASE WHEN (COUNT(DISTINCT course.Id) IS NOT NULL) THEN COUNT(DISTINCT course.Id) ELSE 0 END AS CourseCount,
CASE WHEN (SUM(course.TotalCourses) IS NOT NULL) THEN SUM(course.TotalCourses) ELSE 0 END AS TotalCourses,
CASE WHEN (SUM(course.Hours) IS NOT NULL) THEN SUM(course.Hours) ELSE 0 END AS Hours
FROM #TempStudent AS #Temp
,并根据你的建议,我试图做到这一点在控制器:
public ActionResult Submit(string name, int[] studentIds, DateTime startDate)
{
context.Database.ExecuteSqlCommand(@"CREATE TYPE dbo_ArrayInt AS TABLE (intValue Int);");
var tvp = new TableValuedParameterBuilder("dbo_ArrayInt", new SqlMetaData("intValue", SqlDbType.Int))
.AddRow(1)
.AddRow(2)
.CreateParameter("StudentIds");
//But here, I'm not sure how to use tvp variable appropriately
var query = _context.Set<StudentModel>().FromSql("dbo.stp_Student @Name = {0}, @StudentIds = {1}, @StartDate = {2}"
, name, studentIds, startDate).ToList();
实体映射的结果:
public class StudentModel
{
[Key]
public long RowId { get; set; }
public int StudentId { get; set; }
public string StudentName { get; set; }
public int CourseCount { get; set; }
[Column(TypeName = "Money")]
public decimal TotalCourses { get; set; }
public double Hours { get; set; }
}
非常感谢! travis – pavilion
不客气:-) –