Running a command although the connection is close adomd

134 views Asked by At

I have read the Mirosoft Document.When we open a connection and then close it, it is possible to use the session.

I have written this block of code to run a command but I get an error message, which says there is no connection. Do you have any Idee how can I close the connection, but use the session to run a cammand:

 try
            {
                using (AdomdConnection adomdConnection = new AdomdConnection("MY Connection String"))
                {
                    adomdConnection.Open();
                    adomdConnection.Close(false);
                    while (true)
                    {
                        String query = @"EVALUATE { BLANK()}";
                        AdomdCommand adomdCommand = new AdomdCommand(query);
                        Console.WriteLine(adomdConnection.SessionID.ToString() + "  " + DateTime.Now.ToString());
                        AdomdDataReader reader = adomdCommand.ExecuteReader();
                        reader.Close();
                        System.Threading.Thread.Sleep(30000);
                    }

                }
            }
            catch(AdomdConnectionException ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
1

There are 1 answers

0
Rrrrrr On

In the examples shown in the document you list, it has:

/*First, try to connect to the specified data source.  
If the connection string is not valid, or if the specified  
provider does not support sessions, an exception is thrown. */  
objConnection.ConnectionString = connectionString;  
objConnection.Open();  
  
// Now that the connection is open, retrieve the new  
// active session ID.  
strSessionID = objConnection.SessionID;  
// Close the connection, but leave the session open.  
objConnection.Close(false);  
return strSessionID;  

And in your code specifically, you have:

adomdConnection.Open();
adomdConnection.Close(false);
while (true)
{
    String query = @"EVALUATE { BLANK()}";
    AdomdCommand adomdCommand = new AdomdCommand(query);
    Console.WriteLine(adomdConnection.SessionID.ToString() + "  " + 
    DateTime.Now.ToString());
    AdomdDataReader reader = adomdCommand.ExecuteReader();
    reader.Close();
    System.Threading.Thread.Sleep(30000);
}

Wouldn't you want to have this instead (based on the example given)?

adomdConnection.Open();
while (true)
{
    String query = @"EVALUATE { BLANK()}";
    AdomdCommand adomdCommand = new AdomdCommand(query);
    Console.WriteLine(adomdConnection.SessionID.ToString() + "  " + 
    DateTime.Now.ToString());
    AdomdDataReader reader = adomdCommand.ExecuteReader();
    reader.Close();
    System.Threading.Thread.Sleep(30000);
}
adomdConnection.Close(false);

It seems as though it's complaining because you're closing the connection before you even use it, according to the order in which your code looks to be operating. Try moving the adomdConnection.Close(false); after your while loop.