How to select a SQLConnection from SQLConnections[] by its "Name" property?


When creating a SQLConnection you can give it a name with the "Name" option. The connections are also added to the output of the SQLConnections function.

  JDBC["HSQL(Standalone)", ToFileName[{$TemporaryDirectory, "SE Question"}, "Ex1"] ], 
   "Name" -> "Example1"];
  JDBC["HSQL(Standalone)", ToFileName[{$TemporaryDirectory, "SE Question"}, "Ex2"] ], 
    "Name" -> "Example2"];

Mathematica graphics

Later on I would like to select a particular connection from SQLConnections based on its name. However, SQLConnectionInformation does not have "Name" as supported property

SQLConnectionInformation[#, "Name"] & /@ SQLConnections[]


{$Failed, $Failed}

and there does not seem to be a name property on the connections


Mathematica graphics

and SQLConnections does not take a name parameter


How do I select a connection from SQLConnections by its "Name" property?


CloseSQLConnection /@ SQLConnections[];


WRI Support have provided a method to get at the "Name". I have also requested that a SQLConnection object support "Name" and "Id" as properties.

"Name" is an option. Therefore,

"Name" /. Options[conn, "Name"]

will provide conn's "Name" option.

For the "Id" property WRI said it is the third part of its FullForm. Therefore,


will provide conn's "Id" property. This method seems particularly risky since there is no guarantee that this position will contain this property in future versions.

Since you can see these values in the Format form of a SQLConnection in a notebook I think it is reasonable to expect to be able to get at these values without workarounds. Particularly since there is no other means of selecting a connection out of the SQLConnections list. Fingers are optimistically crossed.


Posted 2017-04-13T18:16:52.003

Reputation: 35 657



The Name option is not unique, so we could be more than one result, if we ask all connections with a particular name. Try this:

connName = "Example2";
 StringMatchQ["Name" /. Cases[Flatten[List @@ #], _Rule],
   connName] &

The Select should give us all of connections with name "Example2".

How it works: We take a single connection and replace the head with List, then Flatten the list. We are interested only in the Rules associated with connection. We apply all of the replacement rules to "Name", which gives us the string name of the connection, assuming there is one. If that string matches the one we want, we select the connection.


Posted 2017-04-13T18:16:52.003

Reputation: 8 428

Nice work. I didn't think to deconstruct the connection. +1 while we wait for other solutions. – Edmund – 2017-04-13T19:23:09.713