Use the SQL Server ODBC driver and the node-odbc interface to connect Node.js to Microsoft SQL Server. You can then access data stored in SQL Server from your Node.js application on Linux and UNIX platforms.
The SQL Server ODBC driver is available for 32-bit and 64-bit Linux and UNIX (AIX, HP-UX and Solaris) platforms.
The unixODBC Driver Manager is included with most Linux distributions and some UNIX distributions. You need both the Driver Manager and its development headers (the package for this may be called unixODBC-dev or unixODBC-devel).
The SQL Server ODBC driver distribution includes both unixODBC and its header files. It's possible to build node-odbc against the Driver Manager files included in the SQL Server ODBC distribution, although the default behaviour is to use the unixODBC files that come with the Operating System.
If your client machine is 64-bit, refer to this note, before downloading the driver.
If the SQL Server ODBC driver is not currently available for your platform, check the list of ODBC-ODBC Bridge Client platforms. The ODBC-ODBC Bridge is an alternative SQL Server solution from Easysoft, which you can download from this site.
For installation instructions, see the ODBC driver documentation. Refer to the documentation to see which environment variables you need to set (LD_LIBRARY_PATH, LIBPATH, LD_RUN_PATH, SHLIB_PATH depending on the driver, platform and linker).
[MSSQL-NODE-JS-DSN] Driver = Easysoft ODBC-SQL Server Server = my_machine\SQLEXPRESS User = my_domain\my_user Password = my_password # If the database you want to connect to is the default # for the SQL Server login, omit this attribute Database = Northwind
cd /usr/local/easysoft/unixODBC/bin ./isql -v MSSQL-NODE-JS-DSN
At the prompt, type "help" to display a list of tables. To exit, press return in an empty prompt line.
If you are unable to connect, refer to this article and the SQL Server ODBC Driver Knowledge Base for assistance.
To do this, do one of the following:
$ mkdir ~/node_odbc_dir $ cd ~/node_odbc_dir $ git clone git://github.com/wankdanker/node-odbc.git
$ mkdir ~/node_odbc_dir $ cd $ svn co https://github.com/wankdanker/node-odbc/trunk node_odbc_dir
node-gyp
to configure and build node-odbc. For example:
$ cd ~/node_odbc_dir $ /usr/local/lib/node_modules/npm/bin/node-gyp-bin/node-gyp configure build
npm
to install node-odbc. For example:
$ cd .. $ sudo npm -g install node_odbc_dir
To test that you can connect to SQL Server from Node.js and fetch some data, use the sample CLI application that is included with node-odbc.
$ cd ~/node_odbc_dir/test $ vi common.js
//exports.connectionString = "Driver={SQLite3};DATABASE=data/sqlite-test.db"; exports.connectionString = "DSN=MSSQL-NODE-JS-DSN";
$ cd ~/node_odbc_dir/test $ NODE_PATH=/usr/local/lib/node_modules/odbc/node_modules/bindings \ LD_LIBRARY_PATH=/usr/local/lib:/usr/local/easysoft/sqlserver/lib:/usr/local/easysoft/lib:$LD_LIBRARY_PATH \ node sql-cli.js $ select 1 as "COLINT", 'some test' as "COLTEXT"; [ { COLINT: 1, COLTEXT: 'some test' } ]
export http_proxy=http://myproxyserver:8080
$ file build/Release/odbc_bindings.node build/Release/odbc_bindings.node: ELF 64-bit LSB shared object, AMD X86-64, version 1 (SYSV), not stripped
When node-odbc is 64-bit, it needs to be built against a 64-bit version of unixODBC and used with a 64-bit version of the SQL Server ODBC driver.
If you built node-odbc against unixODBC 2.2.13 or later (odbcinst --version
), node-odbc will have been built with 64-bit SQLLEN types and you need to use it with a 64-bit SQLLEN version of the SQL Server ODBC driver. (Unless you set the BUILD_LEGACY_64_BIT_MODE
flag when building unixODBC, in which case you need to use node-odbc with a 32-bit SQLLEN version of the SQL Server ODBC driver.)
If you built node-odbc against unixODBC 2.2.12 or earlier, node-odbc will have been built with 32-bit SQLLEN types and you need to use it with a 32-bit SQLLEN version of the SQL Server ODBC driver. (Unless you set the BUILD_LEGACY_64_BIT_MODE
flag when building unixODBC, in which case you need to use node-odbc with a 64-bit SQLLEN version of the SQL Server ODBC driver.)
If you are not sure which version of the SQL Server ODBC driver to use, download the 64-bit version of the driver from the Easysoft web site and build node-odbc against the unixODBC Driver Manager that is included with the SQL Server ODBC driver distribution. You can then be sure that node-odbc, unixODBC and the SQL Server ODBC driver will all be the same architecture and have the same size SQLLEN types. To build node-odbc against the unixODBC Driver Manager included with the driver distribution, you need to:
CXXFLAGS
environment variable:
$ export CXXFLAGS=-Ieasysoft_installation_dir/easysoft/unixODBC/include
Replace easysoft_installation_dir with the directory under which you installed the SQL Server ODBC driver. The default installation directory is /usr/local
.
LDFLAGS
environment variable. For example:
$ export LDFLAGS=-L/usr/local/easysoft/unixODBC/lib
$ cd ~/node_odbc_dir $ rm -rf build $ CXXFLAGS=-I/usr/local/easysoft/unixODBC/include \ LDFLAGS=-L/usr/local/easysoft/unixODBC/lib \ /usr/local/lib/node_modules/npm/bin/node-gyp-bin/node-gyp configure build $ ldd odbc_bindings.node linux-vdso.so.1 => (0x00007fff5172e000) libodbc.so.1 => /usr/local/easyoft/unixODBC/lib/libodbc.so.1 (0x00007ff9e42bf000)