asp.net.ph

SqlCommand.ExecuteXmlReader Method

System.Data.SqlClient Namespace   SqlCommand Class


Sends the CommandText to the Connection and builds an XmlReader object.

[ VB ]
Public Function ExecuteXmlReader ( ) As XmlReader

[ C# ]
public XmlReader ExecuteXmlReader ( );

[ C++ ]
public: XmlReader* ExecuteXmlReader ( );

[ JScript ]
public function ExecuteXmlReader ( ) : XmlReader;

Return Value

An XmlReader object.

Remarks

The CommandText property usually specifies a Transact-SQL statement with a valid FOR XML clause. However, CommandText can also specify a statement that returns ntext data containing valid XML.

A typical ExecuteXmlReader query can be formatted as in the following example:

SqlCommand mySqlCommand = new SqlCommand (
   "SELECT * FROM Customers FOR XML AUTO, XMLDATA", myConn );
  C# VB

While the XmlReader is in use, the associated SqlConnection is busy serving the XmlReader. While in this state, no other operatons can be performed on the SqlConnection other than closing it. This is the case until the Close method of the XmlReader is called.

Example

The following example initializes an SqlCommand and then executes it using ExecuteXmlReader. The example is passed a string that is a Transact-SQL FOR XML SELECT statement, and a string to use to connect to the data source.

void ExecuteXmlReaderDemo ( string query, SqlConnection myConn ) {
   SqlCommand myCommand = new SqlCommand ( query, myConn );

   try {
      myCommand.Connection.Open ( );

      // set up dataset and fill with xml data.
      DataSet myDataSet = new DataSet ( );
      myDataSet.ReadXml ( myCommand.ExecuteXmlReader ( ), XmlReadMode.Fragment );

      // write data to file myData.xml
      myDataSet.WriteXml ( "myData.xml" );
   }
   catch ( Exception e ) {
      Response.Write ( e.ToString ( ) );
   }
   finally {
      myConn.Close ( );
   }
}
  C# VB

See Also

SqlCommand Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph