sqlcommand.ExecuteXmlReader is not work, why?

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top