Databases Reference
In-Depth Information
NOTE
Because ExecuteXmlReader reads data directly from a stream based on the connec-
tion to the server, XmlReader must be closed before any other operations on this con-
nection can be executed. Otherwise, it blocks the work of other commands, and an
exception is thrown.
ExecuteNonQuery
The ExecuteNonQuery method is used to execute Create , Drop , Update , Call , and other
requests that do not return results, but change the state on the server. Unlike the
SqlClient provider, this method never returns a result and cannot be used for a direct call
to stored procedures (see Listing 33.17).
LISTING 33.17
Executing Commands That Do Not Return Results
//Connect to the server.
AdomdConnection con = new AdomdConnection();
con.ConnectionString= “Datasource=localhost; Initial Catalog=Foodmart 2008”;
con.Open();
//Create a command that will send a request for a creation of a calculated member.
String cmdText = “CREATE Member [Warehouse and Sales].Measures.MyCalcMember as 1 “;
AdomdCommand command = new AdomdCommand(cmdText,con);
//Execute the command, without expecting a returned result.
command.ExecuteNonQuery();
//Close the connection.
con.Close();
The result of this code is a calculated member, created on the server and accessible to all
the commands of the current session.
Execute
Use of the methods described in the preceding sections requires that the client application
be aware of the type of request sent. There are a number of client applications for which
this does not present a problem—such an application generates the query and, therefore,
is aware of its type before it sends the query to the server.
However, there are other applications that accept the text of requests from the user, or
from other components of the system, and cannot determine what type of request should
be executed. For such applications, we have the method Execute , which returns a result as
a loosely typed object. Let's look at an example of the use of Execute in Listing 33.18.
 
Search WWH ::




Custom Search