Load Query Layer via AddLayerFromFile() difference: ArcGIS 10.1 Final vs. Prerelease

2509
2
07-01-2012 11:12 AM
ScottKutz
New Contributor II
Environment: ArcGIS Engine Runtime 10.1, Visual Studio 2010, Visual C++

Testing of ArcGIS Engine Runtime 10.1 Final has uncovered a difference in the behavior of the IMapControl4::AddLayerFromFile() between the 10.1 Prerelease and 10.1 Final versions when loading a Query Layer whose definition was saved in a *.lyr file.

It is during execution of IMapControl4::AddLayerFromFile() where the change is seen, but it seems the ISqlWorkspace or ILayerFile interfaces may be involved, since the processing is associated with creating and persisting the definition for a Query Layer.

The difference in behavior is the following:

- In ArcGIS 10.0 and ArcGIS 10.1 Prerelease, loading the saved *.lyr file for the Query Layer used the credentials (userid and password) saved in the *.lyr file to establish the connection to the underlying Oracle 11g database. The user was not prompted.

- In ArcGIS 10.1 Final (after rebuilding our application), executing the same code to define the Query Layer and save its definition into a *.lyr file, ArcGIS now displays a prompt for credentials when executing the IMapControl4::AddLayerFromFile()file, as shown in the attached file ArcGIS_10_1_lyr_file_prompt.png.

We have searched the ArcGIS 10.1 ???What???s new???-type online topics but, so far, have not located anything to help explain this change in behavior.

We are trying to understand if:
- This is expected behavior with ArcGIS 10.1 Final and we just have not yet located the documentation that explains the reason for this change(?)
- Or, is this some type of bug (either in the ArcGIS code or something we might be doing wrong in our ArcObjects code)(?)

We would appreciate any insights others might have.

Thanks for your help.

Scott
0 Kudos
2 Replies
ScottKutz
New Contributor II
Continued investigation has shed some light on this problem and we wanted to post it here to hopefully save other ArcEngine developers from wasting a lot of time on Query Layer connection differences in ArcGIS 10.1.

It appears that the difference is not in the layer (*.lyr) file processing itself, but a difference in the manner for making connections to support Query Layers starting in ArcGIS 10.1.

While no specific �??What�??s new�?��?� documentation on the change has yet been located, it appears that Esri is no longer using the SqlWorkspaceFactory to make the connection for Query Layer processing in ArcGIS 10.1.

Please see the attached file Sql_Workspace_Doc_Differences.png which shows that, while a very useful ArcObjects example was provided in the 10.0 Developer Help topic �??Working with SQL Workspaces�?�, that Help topic has been removed from Developer Help in ArcGIS 10.1.

The attached file Query_Layer_Connection_info.png contrasts the type of dialog used by ArcMap for users to define Query Layer connection properties. It also appears that, while the ArcObjects samples for the SqlWorkspaceFactory class had the connection data being saved to *.qcf files in ArcGIS 10.0, the ArcGIS 10.1 approach indicates the connection data should be saved in *.sde files.

One gets the impression that, starting with ArcGIS 10.1, Esri wants applications to always use the SdeWorkspaceFactory class when establishing a connection, whether the connection is for a geodatabase or not (such as when creating a connection for Query Layers).

Back to our original posting, we have been able to avoid the �??prompt for credentials�?� in our ArcEngine-based application by making the following changes to our code that creates the connection and eventually obtains a ISqlWorkspace interface pointer that is used to create the Query Class that forms the basis for creating a Query Layer:
- Replace use of the SqlWorkspaceFactory with the SdeWorkspaceFactory class.
- In the PropertySet of connection properties, replace the SERVERINSTANCE property (from ArcGIS 10.0) with the DB_CONNECTION_PROPERTIES property (apparently, this is a new property at the ArcGIS 10.1 level).

A Visual C++ sample code fragment (with all error checking removed) is shown below.

  IPropertySetPtr             pPropertySet = NULL;
  IWorkspaceFactory2Ptr       pWorkspaceFactory2 = NULL; 
  IWorkspacePtr               pWorkspace = NULL;
  ISqlWorkspacePtr            pSqlWorkspace = NULL;
  OLE_HANDLE                      hWnd = 0;
  HRESULT                     hr = S_OK;

  // Create the connection Property Set 
  pPropertySet.CreateInstance(CLSID_PropertySet);

  // At ArcGIS 10.0, this parameter specified either "oracle10g" or "oracle11g"
  //    (when used with the SqlWorkspaceFactory class).
  // At ArcGIS 10.1, this parameter is apparently to just be "oracle"
  //    (when used with the SdeWorkspaceFactory class).
  hr = pPropertySet->SetProperty( CComBSTR(_T("DBCLIENT")), 
                                  CComVariant(CComBSTR(_T("oracle"))) );

  // This appears to be a new property at the ArcGIS 10.1 level.
  //    It appears to replace the "SERVERINSTANCE" propety used in ArcGIS 10.0 
  //    to call the ->Open() method on the SqlWorkspaceFactory.
  hr = pPropertySet->SetProperty( CComBSTR(_T("DB_CONNECTION_PROPERTIES")), 
                                  CComVariant(CComBSTR(_T("sde:oracle11g:hydra04"))) );

  hr = pPropertySet->SetProperty( CComBSTR(_T("AUTHENTICATION_MODE")), 
                                  CComVariant(CComBSTR(_T("DBMS"))) );

  hr = pPropertySet->SetProperty( CComBSTR(_T("USER")), 
                                  CComVariant(CComBSTR(_T("scott"))) );

  hr = pPropertySet->SetProperty( CComBSTR(_T("PASSWORD")), 
                                  CComVariant(CComBSTR(_T("zzzzzzz"))) );

  // Create the SDE Workspace factory
  pWorkspaceFactory2.CreateInstance(CLSID_SdeWorkspaceFactory);

  // Open the SDE workspace with the PropertySet
  hr = pWorkspaceFactory2->Open(pPropertySet, hWnd, &pWorkspace); 

  // QI to get the ISqlWorkspace interface pointer.
  //   Once gaining access to this ISqlWorkspace interface pointer,
  //   the rest of the processing for creating a Query Layer seems to be
  //   consistent with the ArcGIS 10.0 Query Layer processing.
  pSqlWorkspace = pWorkspace;


We hope this information is helpful to other developers.

Scott
0 Kudos
BarbaraSchneider1
New Contributor III
Hello Scott,

this is exactly what I was looking for!

Barbara
0 Kudos