private void UpdateDBConnection(string msdFilepath, string sde) { IWorkspaceFactory2 wf = new SdeWorkspaceFactoryClass(); IWorkspace ws = wf.OpenFromFile(sde, 0); IDataset ds = (IDataset)ws; IWorkspaceName2 wn = (IWorkspaceName2)ds.FullName; string conn = wn.ConnectionString; IMSDHelper helper = new MSDHelper(); helper.Open(msdFilepath); var maps = helper.GetMaps(); for (var i = 0; i < maps.Count; i++) { var layers = helper.GetLayers(maps.Element[i]); for (var j = 0; j < layers.Count; j++) { helper.PutWorkspaceConnectionStringInLayer(layers.Element[i], conn, false); } } }
private void printSDEConnections(string sde) { try { // We use IWorkspacename2 so that it returns a ENCRYPTED_PASSWORD required // for the connection string in an MSD. IWorkspaceFactory2 wf = new SdeWorkspaceFactoryClass(); IWorkspace ws = wf.OpenFromFile(txtSDE.Text, 0); IDataset ds = (IDataset)ws; IWorkspaceName2 wn = (IWorkspaceName2)ds.FullName; List <string> dbconns = wn.ConnectionString.Split(';').ToList <string>(); foreach (string param in dbconns) { this.richTextBox2.Text += param + "\n"; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
} // ConnectToGDB private static void UpdateDatalayerInfo(ITable table, Boolean verbose) { String server = ""; String dbclient = ""; String instanceOrig = ""; String instanceNew = ""; IDataLayer dataLayer = null; IRasterLayer rasterLayer = null; if (table is IRasterLayer) { rasterLayer = (IRasterLayer)table; dataLayer = (IDataLayer)rasterLayer; } else { dataLayer = (IDataLayer)table; } IName n = dataLayer.DataSourceName; IDatasetName dsn = (IDatasetName)n; IWorkspaceName2 wsn = (IWorkspaceName2)dsn.WorkspaceName; IWorkspaceFactory2 wsFact = (IWorkspaceFactory2)wsn.WorkspaceFactory; IWorkspace2 workspace = (IWorkspace2)wsFact.OpenFromString(wsn.ConnectionString.ToString(), 0); // Access connection properties, update, and re-apply IPropertySet propertySet = wsn.ConnectionProperties; // Confirm original server = propertySet.GetProperty("SERVER").ToString(); instanceOrig = propertySet.GetProperty("INSTANCE").ToString(); if (verbose) { Console.WriteLine("Original: {0}, {1}", server, instanceOrig); } // Only need to set if currently app server, check if "instance" string has a colon in it // Is there a way to check this based on the list by source so that we don't have to check every layer? if (instanceOrig.Contains(":") == false) { // Determine what kind of DBMS we are working with (Oracle, SqlServer, etc.) IDatabaseConnectionInfo3 dbmsConnInfo = (IDatabaseConnectionInfo3)workspace; // Set the DBCLIENT to the appropriate dbms switch (dbmsConnInfo.ConnectionDBMS) { case esriConnectionDBMS.esriDBMS_Oracle: dbclient = "Oracle"; instanceNew = String.Format("{0}/{1}", dbmsConnInfo.ConnectionServer, dbmsConnInfo.ConnectedDatabaseEx); break; case esriConnectionDBMS.esriDBMS_SQLServer: dbclient = "SQLServer"; instanceNew = dbmsConnInfo.ConnectionServer; break; case esriConnectionDBMS.esriDBMS_PostgreSQL: dbclient = "PostgreSQL"; instanceNew = dbmsConnInfo.ConnectionServer; break; default: dbclient = ""; break; } // Set to new target propertySet.SetProperty("DBCLIENT", dbclient); propertySet.SetProperty("DB_CONNECTION_PROPERTIES", instanceNew); // INSTANCE needs to be cleared out for Raster layers if (table is IRasterLayer) { propertySet.SetProperty("INSTANCE", "");//propertySet.SetProperty("INSTANCE", String.Format("sde:{0}",dbclient.ToLower())); } wsn.ConnectionProperties = propertySet; // Confirm target was set propertySet = null; propertySet = wsn.ConnectionProperties; if (verbose) { Console.WriteLine("NEW: {0}, {1}", propertySet.GetProperty("DBCLIENT"), propertySet.GetProperty("DB_CONNECTION_PROPERTIES")); Console.WriteLine("----------------"); } } // End if } // UpdateDatalayerInfo