sqlcommand.ExecuteXmlReader is not work, why?

  • Thread starter Thread starter Risen
  • Start date Start date
R

Risen

Hi,

When I use sqlcommand.ExecuteXmlReader to receive Sql Server 2005's table
data, it not work! my code is below:

private static void CreateXMLReader(string queryString,string
connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
System.Xml.XmlReader reader = command.ExecuteXmlReader();
}
}

private void button3_Click(object sender, EventArgs e)
{
CreateXMLReader("select * from testtable for xml auto", "Data
Source=SQL2005;Initial Catalog=Test;Integrated Security=True");
}

The reader can be created successfully, and debugger didn't report error,
but the reader is None!! It contains nothing!! Why?? Is there any mistake in
my code? please help me. Thanks.


Risen
 
When you say "the reader is None", are you sure? Try something like so:

StringBuilder sb = new StringBuider();
while (!reader.EOF)
if (reader.IsStartElement())
{
sb.Append(reader.ReadOuterXml());
ctr++;

}
reader.Close();

Response.Write(sb.ToString());
// or Console.Write(sb.ToString());

Peter
 
Back
Top