I have a Sql SP that takes around 1 minute to run, returning 25,000 rows of data. (multiple datasets can be returned).
Currently trying to convert this into an XElement/XDocument to produce several reports results in the c# method converting this taking over 30 minutes, which requires a Sql Connection\Command Timeout of 30 minutes, which is just far far too long.
Can anyone help troubleshoot/find where i can make improvements to the following conversion code, as there must be a huge inefficiency in here somewhere.
The Call
public void xyzCall()
{
....
XElement result = SqlDataReadertoXML(sqlcommand.ExecuteReader());
....
}
The Conversion Function
private XElement SqlDataReadertoXML(SqlDataReader datareader)
{
XElement results = new XElement("ResultSets");
// Read Next RecordSet
do
{
XElement result = new XElement("ResultSet");
//Read Next Row in this RecordSet
while (datareader.Read())
{
XElement datanode = new XElement("Item");
// Read Each Column in this RecordSet
for (int i = 0; i < datareader.FieldCount; i++)
{
// Node.Attr("Name") = Column Name, Node.Value = Field
if (datareader.GetName(i) != "") datanode.Add(new XElement(datareader.GetName(i), datareader[i].ToString()));
}
result.Add(datanode);
}
results.Add(new XElement(result));
} while (datareader.NextResult());
datareader.Close();
return results;
}