Change Query for Query Layer with code

3045
4
12-19-2012 01:18 AM
jamalhanine
New Contributor
i currently have projects that add a map layer from our sql postgres (Postgis) to a project.

my problem is how to change the connection of datasource (connection with postgres) by code (arcobject .net).

i think taht i shoud get the query of the query layer and relodit by the new connection with the new parametrs

but how can i get the query for the query layer to do that?

would anyone have any code they could share to show how someone would use .?

thanks!
0 Kudos
4 Replies
jamalhanine
New Contributor
I found the solution for the problem, and i want to share it with you, if never some one has the same problem

Dim pTable As ITable = featureLayer.FeatureClass
Dim pDataset As IDataset = pTable
Dim pQueryTableName As IQueryTableName
pQueryTableName = pDataset.FullName
Dim query As String = pQueryTableName.QueryDescription.Query

Jamal Hanine
0 Kudos
georgandersson
New Contributor II

Hi

does anyone know, how the querylayer itself is created? i need to do exactyl the same as described (change the source of an existing querylayer). so the solution for getting the information of the existing layer is allready provided. but how do i create the new querylayer from the new source?

best regards

george

seria
by Esri Contributor
Esri Contributor

Hello,

Here is some sample code that shows how to create a Query Layer:

IWorkspaceFactory2 workspaceFactory = new SqlWorkspaceFactoryClass();

string connectionString = "dbclient=oracle11g;" + "serverinstance=190.100.100.200/MYMAP;" + "USER=SDE;" + "PASSWORD=SDE";
ISqlWorkspace sqlWorkspace = workspaceFactory.OpenFromString(connectionString, 0) as ISqlWorkspace;
IQueryDescription queryDescription = sqlWorkspace.GetQueryDescription("SELECT * FROM SDE.MYTABLE");

ITable table = pSqlWorkspace.OpenQueryClass("TestQueryTable", queryDescription);
IFeatureClass featureClass = table as IFeatureClass;
featureLayer.FeatureClass = featureClass;
axMapControl1.Map.AddLayer(pFeatureLayer as ILayer);

This is based on code posted by a user on another forum (https://code.csdn.net/snippets/229166).

0 Kudos
DanNarsavage_IDWR
New Contributor III

In case it saves anyone else time in the future if they try using that snippet . . .  The SqlWorkspaceFactory class doc says . . .

The SDEWorkspaceFactory.Open method should be used to make connections to all databases and enterprise geodatabases.

0 Kudos