DSN (Data Source Name)

The DSN is a way of referring to a particular driver and database by any name you wish. The DSN is usually a key to a list of attributes the ODBC driver needs to connect to the database (e.g. ip address and port) but there is always a key which names the driver so the driver manager knows which driver to use with which data source. Do not confuse DSNs with ODBC connection strings or DBI’s “$data_source” (the first argument to “connect” in DBI.

The $data_source argument to DBI is composed of ‘dbi:DRIVER:something_else’ where DRIVER is the name of the DBD driver you want to use (ODBC of course for DBD::ODBC). The “something_else” for DBD::ODBC can be a DSN name or it can be a normal ODBC connection string.

An ODBC connection string consists of attribute/value pairs separated with semicolons (;). You can replace “something_else” above with a normal ODBC connection string but as a special case for DBD::ODBC you can just use the DSN name without the usual ODBC connection string prefix of “DSN=dsn_name”.

e.g.

dbi:ODBC:DSN=fred
ODBC connection string using fred DSN

dbi:ODBC:fred
Same as above (a special case).

dbi:ODBC:Driver={blah blah driver};Host=1.2.3.4;Port=1000;
This is known as a DSN-less connection string for obvious reasons.

Leave a Reply

You must be logged in to post a comment.