Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement for DataTable/DataSet in .NET Core (NET Standard 2.1)

.NET Core 3.1 supports DataTable/DataSet, DataRow, DataView, etc - which was not available when the OP created the following question

based on my research, it seems like .net core does not support DataTable/DataSet. I recently shifted to .net core for developing a new application, but failed to recognize that .net core does not have library for those in the first place. I used to use dt/ds for fetching the data via stored procedure and fill collection of classes. But now, I am quite lost with this new problem that I have to find replacement for dt/ds. To be more specific, I used to do this:

I have a stored procedure that takes four input parameters and returns a table.

ALTER PROCEDURE stp_Student

@Name nvarchar(450), 
@StudentIds tvp_ArrayInt READONLY,  
@StartDate date,
@EndDate 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 create a class with same fields as I have in my stored procedure.

public class StudentModel
{  
    public string 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; }
}

and fetch data and fill collection of class

using (SqlConnection conn = new SqlConnection(connectionString)) 
{
    SqlCommand cmd = new SqlCommand("sp_Student", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Name", name);
    cmd.Parameters.AddWithValue("@StudentIds", studentIds);
    cmd.Parameters.AddWithValue("@StartDate", startDate);
    cmd.Parameters.AddWithValue("@EndDate", endDate);
    conn.Open();

    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    da.Fill(ds);
    conn.Close();
}

foreach (Datarow row in ds.Tables[0].Rows)
{
    var model = new StudentModel();
    model.StudentId = Convert.ToString(row["StudentId"]);
    //etc..
    studentList.Add(model);
}

My bad, I should have researched more before I initiated the project, but I really need anyone's help to find replacement for the above code. Would it be possible to use .net core's FromSql method as a replacement? Any suggestion or sample codes would be appreciated.

Edit

According to Peter's article, found a way to implement ds/dt in .net core version, but having a bit problem passing an int array(StudentIds) to my stored procedure.

public List<StudentModel> GetStudents(string name, IEnumerable<int> studentIds, DateTime startDate, DateTime endDate)
    {
        List<StudentModel> students = new List<StudentModel>();
        StudentModel = null;

        List<DbParameter> parameterList = new List<DbParameter>();

        parameterList.Add(base.GetParameter("Name", name));
        parameterList.Add(base.GetParameter("StudentIds", studentIds));           
        parameterList.Add(base.GetParameter("StartDate", startDate));
        parameterList.Add(base.GetParameter("EndDate", endDate));       

        using (DbDataReader dataReader = base.ExecuteReader("stp_Student", parameterList, CommandType.StoredProcedure))
        {
            if (dataReader != null)
            {
                while (dataReader.Read())
                {
                    model = new StudentModel();
                    model.StudentId= (int)dataReader["StudentId"];
                    ....

                    students .Add(model);
                }
            }
        }
        return students;
    }
}

I think the problem comes from passing IEnumerable? How can I pass an int array to stored procedure correctly?

like image 371
bbusdriver Avatar asked Feb 18 '17 09:02

bbusdriver


People also ask

Which is better DataSet or DataTable?

There is no much difference between dataset and datatable, dataset is simply the collection of datatables.

What is difference between DataTable and DataSet?

A DataTable object represents tabular data as an in-memory, tabular cache of rows, columns, and constraints. The DataSet consists of a collection of DataTable objects that you can relate to each other with DataRelation objects.

What is DataSet in C#?

Introduction to Dataset in C# DataSet is a disconnected architecture it represents the data in table structure which means the data into rows and columns. Dataset is the local copy of your database which exists in the local system and makes the application execute faster and reliable.


2 Answers

DataTable now exists in .NET Core 2.0. See my answer at https://blogs.msdn.microsoft.com/devfish/2017/05/15/exploring-datatable-and-sqldbadapter-in-asp-net-core-2-0/ .

public static DataTable ExecuteDataTableSqlDA(SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
 {
 System.Data.DataTable dt = new DataTable();
 System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmdText, conn);
 da.Fill(dt);
 return dt;
 }
like image 122
Joe Healy Avatar answered Nov 16 '22 01:11

Joe Healy


I don't know much about .Net Core, but I think you can use IEnumerable<SqlDataRecord> instead.

like image 34
Abdullah Dibas Avatar answered Nov 16 '22 02:11

Abdullah Dibas