sqlcommand.ExecuteXmlReader is not work, why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Risen

    sqlcommand.ExecuteXmlReader is not work, why?

    Hi,

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

    private static void CreateXMLReader (string queryString,str ing
    connectionStrin g)
    {
    using (SqlConnection connection = new SqlConnection(c onnectionString ))
    {
    connection.Open ();
    SqlCommand command = new SqlCommand(quer yString, connection);
    System.Xml.XmlR eader reader = command.Execute XmlReader();
    }
    }

    private void button3_Click(o bject sender, EventArgs e)
    {
    CreateXMLReader ("select * from testtable for xml auto", "Data
    Source=SQL2005; Initial Catalog=Test;In tegrated 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


  • Peter Bromberg [C# MVP]

    #2
    RE: sqlcommand.Exec uteXmlReader is not work, why?

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

    StringBuilder sb = new StringBuider();
    while (!reader.EOF)
    if (reader.IsStart Element())
    {
    sb.Append(reade r.ReadOuterXml( ));
    ctr++;

    }
    reader.Close();

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

    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:

    UnBlog:





    "Risen" wrote:
    Hi,
    >
    When I use sqlcommand.Exec uteXmlReader to receive Sql Server 2005's table
    data, it not work! my code is below:
    >
    private static void CreateXMLReader (string queryString,str ing
    connectionStrin g)
    {
    using (SqlConnection connection = new SqlConnection(c onnectionString ))
    {
    connection.Open ();
    SqlCommand command = new SqlCommand(quer yString, connection);
    System.Xml.XmlR eader reader = command.Execute XmlReader();
    }
    }
    >
    private void button3_Click(o bject sender, EventArgs e)
    {
    CreateXMLReader ("select * from testtable for xml auto", "Data
    Source=SQL2005; Initial Catalog=Test;In tegrated 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
    >
    >
    >

    Comment

    Working...