我需要一种方法来优化从 Oracle 数据库中将数百万条记录的数据检索到自定义业务对象列表中。从 Oracle 返回的数据是 XML 格式,我需要一种将其序列化为业务对象列表的方法。
我编写的代码工作正常,但是,在将 XML 加载到内存期间执行需要很长时间,特别是当代码到达该行时:
var xDoc = XDocument.Load(xmlReader);
代码 :
//Custom Business object
public class AccountType
{
public int AccountTypeID { get; set; }
public string AccountCode { get; set; }
public string BookType { get; set; }
public int Status { get; set; }
}
//Code that retrieves data from Oracle DB
using (OracleConnection objOracleConnection = new OracleConnection(strConnectionString))
{
using (OracleCommand orclCmd = objOracleConnection.CreateCommand())
{
try
{
orclCmd.CommandText = strXMLSQL;
orclCmd.BindByName = true;
orclCmd.XmlCommandType = OracleXmlCommandType.Query;
orclCmd.XmlQueryProperties.RootTag = "AccountData";
orclCmd.XmlQueryProperties.RowTag = "ROW";
objOracleConnection.Open();
XmlReader xmlReader = orclCmd.ExecuteXmlReader();
var xDoc = XDocument.Load(xmlReader);
List<AccountType> accountTypes = (from data in xDoc.Root.Elements("ROW")
select new AccountType
{
AccountTypeID = data.GetIntXMLElementValue("ACCOUNTTYPEID"),
AccountCode = data.GetStringXMLElementValue("ACCOUNTCODE"),
BookType = data.GetStringXMLElementValue("BOOKTYPE"),
Status = data.GetIntXMLElementValue("STATUS")
}).ToList();
}
catch (OracleException oracleEx)
{
throw oracleEx;
}
catch (Exception generalEx)
{
throw generalEx;
}
finally
{
objOracleConnection.Close();
}
}
任何帮助将不胜感激。
谢谢!