So this link, shows how to pass an int array to stored procedure.
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
and c# Code:
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
}
But since .net core does not support datatable, what would be the equivalent code for .net core? Especially adding tvp variable as a parameter in AddWithValue method and also the last line of code which is tvparam.SqlDbType = SqlDbType.Structured?
Edit
So I have this stored procedure that takes three parameters. @StudentIds is an int array.
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
and based on your recommendation, I tried to do it in controller:
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();
Entity for mapping the result:
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; }
}