ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Core.Data Namespace / Datastore Class / GetConnectionString Method
Example Version

GetConnectionString Method
Gets the connection string associated with the currently opened data store (e.g., an enterprise geodatabase). This method must be called on the MCT. Use QueuedTask.Run.
Syntax
public string GetConnectionString()

Return Value

The connection string associated with the currently opened data store (e.g., an enterprise geodatabase).
Exceptions
ExceptionDescription
No valid data store has been opened or the type of data store is currently not supported prior to invoking this operation.
A geodatabase-related exception has occurred.
Example
Change the underlying data source of a feature layer - same workspace type
//This is the existing layer for which we want to switch the underlying datasource
var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
QueuedTask.Run(() =>
{
  var connectionStringToReplace = lyr.GetFeatureClass().GetDatastore().GetConnectionString();
  string databaseConnectionPath = @"Path to the .sde connection file to replace with";
  //If the new SDE connection did not have a dataset with the same name as in the feature layer,
  //pass false for the validate parameter of the FindAndReplaceWorkspacePath method to achieve this. 
  //If validate is true and the SDE did not have a dataset with the same name, 
  //FindAndReplaceWorkspacePath will return failure
  lyr.FindAndReplaceWorkspacePath(connectionStringToReplace, databaseConnectionPath, true);
});
Requirements

Target Platforms: Windows 11, Windows 10

ArcGIS Pro version: 3 or higher.
See Also