Thanks Brian, that was the answer I was looking for. In my case I want to get the workspace of the selected layer so I can check it is the same as the edit workspace.
So to extend it further using VB.NET:
Dim pLayer As ILayer
pLayer = pMxDoc.SelectedLayer 'pMxDoc was created earlier
Dim selectedFL As IFeatureLayer2
selectedFL = pLayer 'QI
'Get the workspace from FL -> FC -> FD
Dim pFeatureClass As IFeatureClass
pFeatureClass = selectedFL.FeatureClass 'The FL was created earlier above
Dim pFeatureDataset As IDataset
pFeatureDataset = pFeatureClass 'QI
Dim pWorkspace As IWorkspace
pWorkspace = pFeatureDataset.Workspace
To answer the question you could cast one step further to IDatabaseConnectionInfo3:
Dim pWSConnectionInfo As IDatabaseConnectionInfo3
pWSConnectionInfo = pWorkspace 'QI
'Lets look at the workspace properties.
MsgBox("Workspace is esriWorkspaceType: " & pWorkspace.Type & vbNewLine & _
"Database is " & pWSConnectionInfo.ConnectionServer & vbNewLine & _
"Database name is " & pWSConnectionInfo.ConnectedDatabaseEx, _
MsgBoxStyle.Information, "Debug check")
gives (for my Oracle sde database)
Workspace is esriWorkspaceType: 2
Database is hostname.fulldomain
Database name is actual dbasename
Naturally you'd need to be careful. Casting IDatabaseConnectionInfo3 to a FGDB won't work and will generate a runtime error unless handled. In my case it is even more complicated as the Oracle TNSNAME is an alias to the actual dbasename, and the users never use the real name only 'DMS' in their GIS applications.