.NET Core 3.1 supports DataTable/DataSet, DataRow, DataView, etc - which was not available when the OP created the following question
根据我的研究,.net core 似乎不支持 DataTable/DataSet。我最近转向 .net core 来开发一个新的应用程序,但未能认识到 .net core 首先没有用于那些的库。我曾经使用 dt/ds 通过存储过程获取数据并填充类集合。但是现在,我对这个必须找到 dt/ds 的替代品的新问题感到很困惑。更具体地说,我曾经这样做过:
我有一个存储过程,它接受四个输入参数并返回一个表。
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
并创建一个与我在存储过程中具有相同字段的类。
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; }
}
获取数据并填充类的集合
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);
}
我的错,我应该在启动该项目之前进行更多研究,但我真的需要任何人的帮助才能找到上述代码的替代品。是否可以使用 .net core 的 FromSql 方法作为替代?任何建议或示例代码将不胜感激。
编辑
根据 Peter 的文章,找到了一种在 .net 核心版本中实现 ds/dt 的方法,但是在将 int array(StudentIds) 传递给我的存储过程时遇到了一些问题。
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;
}
}
我认为问题来自传递 IEnumerable?如何正确地将 int 数组传递给存储过程?
最佳答案
DataTable 现在存在于 .NET Core 2.0 中。在 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;
}
关于c# - .NET Core (NET Standard 2.1) 中 DataTable/DataSet 的替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42313211/