Safe Way to Check for IAzManStore

Topics: NetSqlAzMan Core
Developer
Mar 20, 2012 at 7:08 PM

 

If the storeName does not exist...........it throws an exception.

I do not see a .ContainsKey method for IAzManStorage.

 

Any advice?

 

 

            IAzManStore returnStore = null;

            //Create an instance of SqlAzManStorage class
            using (IAzManStorage storage = new SqlAzManStorage(this.SqlAzmanConnectionString))
            {
                //Open Storage Connection
                storage.OpenConnection();
                //Begin a new Transaction
                storage.BeginTransaction(AzManIsolationLevel.ReadUncommitted);


                returnStore = storage.GetStore(storeName); /*Blows up if store does not exist*/

                storage.CommitTransaction();
                //Close connection
                storage.CloseConnection();
            }


            return returnStore;

Developer
Mar 20, 2012 at 7:12 PM

FYI, I tried this as well.

 

                if (null != storage.Stores)
                {
                    if (null != storage.Stores[storeName])
                    {
                        returnStore = storage.GetStore(storeName);
                    }
                }

 

 

Same exception(s).

Developer
Mar 20, 2012 at 7:58 PM

I believe storage.Stores is a Dictionary. It will throw a KeyNotFoundException if storeName doesn't exist.

Try the following code,

	IAzManStore returnStore;
	storage.Stores.TryGetValue(storeName, out returnStore);
	if( returnStore == null)
	{
	}
	else
	{
		// work with returnStore.
	}

 

Developer
Mar 20, 2012 at 8:24 PM

 

BLESS YOU!

 

That has opened up many of my "check if exists" calls.

THANK YOU AGAIN!