如何使用Asp.NetCore中的NReco.Data从StoredProcedure获取多个RecordSet [英] How to get multiple RecordSets from StoredProcedure with NReco.Data in Asp.NetCore
问题描述
我在Asp.NetCore应用程序中使用NReco.Data进行数据库调用,因为我不想使用EF并且还不支持DataTable。
I use NReco.Data in my Asp.NetCore Application to make db-calls, because I don't want to use EF and DataTable isn't supported yet.
现在,我需要调用一个StoredProcedure并获取多个RecordSet(或词典列表)。
Now I need to call a StoredProcedure and get Multiple RecordSets (or Dictionarylists).
此刻,我这样称呼:
dbAdapter.Select($ STOREDNAME @ {{nameof(SQLPARAMETER)},SQLPARAMETER).ToRecordSet()
但是存储给了我更多超过1个记录集,有人可以帮助我获取其他记录集吗?
But the stored gives me more than 1 recordset, can anyone help me to get the others?
推荐答案
当前 NReco.Data.DbDataAdapter
没有用于处理单个 IDbCommand
返回的多个结果集的API。
Currently NReco.Data.DbDataAdapter
has no API for processing multiple result sets returned by single IDbCommand
.
您可以自己编写 IDbCommand
,执行数据读取器并按以下方式读取多个结果集:
You can compose IDbCommand
by yourself, execute data reader and read multiple result sets in the following way:
IDbCommand spCmd; // lets assume that this is DB command for 'STOREDNAME'
RecordSet rs1 = null;
RecordSet rs2 = null;
spCmd.Connection.Open();
try {
using (var rdr = spCmd.ExecuteReader()) {
rs1 = RecordSet.FromReader(rdr);
if (rdr.NextResult())
rs2 = RecordSet.FromReader(rdr);
}
} finally {
spCmd.Connection.Close();
}
作为NReco.Data作者,我认为对多个结果集的支持可能很容易添加到 DbDataAdapter
API中(我刚刚创建了为此问题在github上)。
As NReco.Data author I think that support for multiple result sets may be easily added to DbDataAdapter
API (I've just created an issue for that on github).
-更新-
从NReco.Data v.1.0开始.2可以通过以下方式处理多个结果集:
Starting from NReco.Data v.1.0.2 it is possible to handle multiple result sets in the following way:
(var companies, var contacts) = DbAdapter.Select("exec STOREDNAME").ExecuteReader(
(rdr) => {
var companiesRes = new DataReaderResult(rdr).ToList<CompanyModel>();
rdr.NextResult();
var contactsRes = new DataReaderResult(rdr).ToList<ContactModel>();
return (companiesRes, contactsRes);
});
以相同的方式 DataReaderResult
可以映射结果到字典或 RecordSet
(如果需要)。
In the same manner DataReaderResult
can map results to dictionaries or RecordSet
if needed.
这篇关于如何使用Asp.NetCore中的NReco.Data从StoredProcedure获取多个RecordSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!