ASP.NET Core中的数据表类 [英] Datatable class in asp.net core
问题描述
如果您试图在asp.net核心中使用SQLClient,您可能会注意到缺少用于数据库I / O的表结构DataTables和DataSets。
If you have tried to use SQLClient in asp.net core, you might have noticed the absence of the DataTables and DataSets, tables structures used to I/O of database.
对于输出数据,我们可以选择SqlDataReader。
但是对于输入数据,我还没有找到解决此问题的方法-例如如果您想通过框架461中的参数将表传递给SP,则使用 SqlDbType = SqlDbType.Structured and DataTable类。
任何想法有人吗?
For output data we have the option of SqlDataReader. But for the input data, I'm yet to find a solution to this problem -e.g. if you want pass a table to SP by parameter in framework 461, we use 'SqlDbType = SqlDbType.Structured and DataTable class'. Any ideas anyone?
我使用的库: https://github.com/XML-Travelgate/xtg-data-sqlclient
推荐答案
解决方案:
List<SqlDataRecord> datatable = new List<SqlDataRecord>();
SqlMetaData[] sqlMetaData = new SqlMetaData[2];
sqlMetaData[0] = new SqlMetaData("id", SqlDbType.Int);
sqlMetaData[1] = new SqlMetaData("name", SqlDbType.VarChar, 50);
SqlDataRecord row = new SqlDataRecord(sqlMetaData);
row.SetValues(new object[] { 1, "John" });
datatable.Add(row);
row = new SqlDataRecord(sqlMetaData);
row.SetValues(new object[] { 2, "Peter" });
datatable.Add(row);
var task = dbBase.ExecProcedureDataTableWithParamsAsync<object>("VIEWTABLE", new List<SqlParameter>()
{
new SqlParameter()
{
ParameterName = "@paramtable",
SqlDbType = SqlDbType.Structured,
Direction = ParameterDirection.Input,
Value = datatable
}
});
这篇关于ASP.NET Core中的数据表类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!