Monday, April 12, 2010

XmlReader to Retrieve Multiple Rows

 SqlDatabase dbSQL = DatabaseFactory.CreateDatabase("EntLibQuickStartsSql") 
as SqlDatabase;

string sqlCommand = "SELECT ProductID, ProductName FROM Products FOR XML AUTO";
DbCommand dbCommand = dbSQL.GetSqlStringCommand(sqlCommand);

XmlReader productsReader = null;
StringBuilder productList = new StringBuilder();

try  while (!productsReader.EOF)
  {
    if (productsReader.IsStartElement()) 
    {
      productList.Append(productsReader.ReadOuterXml());
      productList.Append(Environment.NewLine);
    }
  }   
}
finally  if (productsReader != null)
  {
    productsReader.Close();
  }
}
{
  productsReader = dbSQL.ExecuteXmlReader(dbCommand);

 
{