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;
An XmlReader object.
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 );
dim mySqlCommand as 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.
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 ( );
}
}
Public Sub ExecuteXmlReaderDemo ( query As String, myConn As SqlConnection )
Dim myCommand As SqlCommand = New SqlCommand ( query, myConn )
try
myCommand.Connection.Open ( )
' set up dataset and fill with xml data.
Dim myDataSet As DataSet = New DataSet ( )
myDataSet.ReadXml ( myCommand.ExecuteXmlReader ( ), XmlReadMode.Fragment )
' write data to file myData.xml
myDataSet.WriteXml ( "myData.xml" )
catch e As Exception
Response.Write ( e.ToString ( ) )
finally
myConn.Close ( )
end try
End Sub |
|
C# |
VB |
SqlCommand Members