我使用的是.Net 4.5、C#、Oracle11g和EF 4.4.0.0
现有进程
因此,我们有一个类似于底层数据库结构的DataModel和应用程序中作为类创建的适当实体对象。我们并不总是使用EF加载数据,因为有时我们使用存储过程将数据加载到客户端,然后通过记录应用程序中的实体类来映射数据记录。
现有代码
var dataTable = new DataTable();
var accounts = new Dictionary<Decimal, ApplicationDomain.MainView.EMPLOYEE_MASTER>();
using (var cmd = new OracleCommand("PKG_TEST.GetAllEmployees", (OracleConnection)Db.Connection))
{
cmd.Parameters.Add(new OracleParameter("DEPT_ID", userBankId));
cmd.Parameters.Add(new OracleParameter("EMPLOYEE_P", OracleDbType.RefCursor, 0, ParameterDirection.Output, true, 0, 0, string.Empty, DataRowVersion.Default, Convert.DBNull));
cmd.CommandType = CommandType.StoredProcedure;
Db.Open();
using (IDataReader dataReader = cmd.ExecuteReader())
{
try
{
dataTable.Load(dataReader );
foreach (BusinessObjectBuilder<ApplicationDomain.MainView.EMPLOYEE_MASTER> employeeBuilder in
dataTable.Rows.Cast<DataRow>().Select(row => new BusinessObjectBuilder<ApplicationDomain.MainView.EMPLOYEE_MASTER>(row)).Where(
accountBuilder => ((IBusinessObjectBuilder<ApplicationDomain.MainView.EMPLOYEE_MASTER>)employeeBuilder).Value != null))
{
var employee = ((IBusinessObjectBuilder<ApplicationDomain.MainView.EMPLOYEE_MASTER>)employeeBuilder).Value;
try
{
if (!employees.ContainsValue(employee))
{
employees.Add(employee.EMPLOYEE_ID, employee);
}
else
{
_log.InfoFormat("Employee ID already exists for this Department : Employee Id{0}", employee.EMPLOYEE_ID);
}
}
catch { }
}
}
finally
{
if (dataReader != null && !dataReader.IsClosed)
{
dataReader.Close();
}
}
}
}BusinessObjectBuilder类接受每一行EMPLOYEE,并将其与我们在应用程序中定义的实体属性进行映射。目前正在对数据表的每一行执行此操作,在从IDataReader加载其数据之后。
我在找什么
我们希望摆脱DataTable正在装载来自阅读器的数据的部分。我想直接使用BusinessObjectBuilder与来自dataReader对象的行/记录,并将其映射到相应的实体属性。
正如您在上面看到的,流程循环遍历datatable中的每一行,成功地将每条记录映射到一个实体对象中,并将其发送回Dictionary,该对象将被添加到一个EMPLOYEE_ID作为键。
问题
我无法找到一种方法来迭代dataReader对象,获取单独的记录,然后通过BusinessObjectBuilder执行正在完成的过程,并将其添加到Dictionary中,并以EMPLOYEE_ID作为键。
有什么方法可以像我们使用IDataReader一样,以同样的方式或其他方式迭代一个DataTable实例吗?
https://stackoverflow.com/questions/29493533
复制相似问题