Databases Reference
In-Depth Information
ExecuteDataReader
The ExecuteDataReader method executes a command and returns an AdomdDataReader
object, which is used for navigating the information received from the server in the form
of a forward-only recordset. (We discuss the AdomdDataReader object later, in the “Working
with Data in Tabular Format” section.) Listing 33.15 is a simple code example that shows
the use of this method.
LISTING 33.15
Executing a Command and Retrieving a Result in the DataReader Format
// Connect to the server.
AdomdConnection con = new AdomdConnection();
con.ConnectionString = “Datasource = localhost; Initial Catalog=Foodmart 2008”;
con.Open();
// Create a simple MDX statement.
AdomdCommand command = (AdomdCommand)con.CreateCommand();
string query = “SELECT Measures.Members ON COLUMNS FROM [Warehouse and Sales]”;
command.CommandText = query;
//Execute the method that returns IDataReader.
IDataReader reader = command.ExecuteReader();
// Read the metadata about the columns in the returned result.
DataTable schemaTable = reader.GetSchemaTable();
Console.WriteLine(“Schema”);
for(int iSchRow = 0; iSchRow<schemaTable.Rows.Count; iSchRow ++)
{
for(int iSchCol = 0; iSchCol<schemaTable.Columns.Count; iSchCol ++)
{
Console.WriteLine(schemaTable.Rows[iSchRow][iSchCol].ToString() + “ “);
}
}
// Read the result.
while(reader.Read())
{
for(int iCol = 0; iCol <reader.FieldCount; iCol ++)
{
Console.WriteLine(reader.GetValue(iCol).ToString());
Console.WriteLine(reader.GetDataTypeName(iCol));
}
}
// Close DataReader.
reader.Close();
// Close the connection.
con.Close();
 
Search WWH ::




Custom Search