You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Jonathan Guerin edited this page Oct 18, 2012
·
8 revisions
Microsoft Driver for Node.js for SQL Server Quickstart Guide
This document aims to provide you with a quick guide on setting up, deploying, and using the msnodesql driver binary package within your node.js application.
Prerequisites
The msnodesql driver has a few dependencies it needs in order to function.
Node.js 0.6 (minimum version 0.6.10). If you want to use a newer version of node.js, you will need to build the driver from source
SQL Server Native Client 11.0 - available as Microsoft SQL Server 2012 Native Client found in the SQL Server 2012 Feature Pack
The msnodesql driver relies on the underlying SQL Server Native Client 11.0, and will not function without it.
Downloading and installing the driver
You can download the driver at the download page at the Microsoft Download Center.
Once you have downloaded the package, extract it to a temporary directory. Once you have extracted it, execute the install script to build the directory structure:
msnodesql-install.cmd
Once the script has completed successfully, you should have a msnodesql directory. Move this directory to your application's node_modules folder so that the structure looks like so:
application directory
node_modules
msnodesql
lib
Your first 'Hello World' application
Once you have set up the driver and its prerequisites, you should be able to successfully use the driver to connect to SQL Server or Windows Azure SQL Database. The following is an example application utilizing the driver. In this example, we are connecting to a local SQL Server Express instance named ‘SQLEXPRESS’:
// Query with streamingvarsql=require('msnodesql');varconn_str="Driver={SQL Server Native Client 11.0};Server={(local)\\SQLEXPRESS};Database={DBName};Trusted_Connection={Yes};";varstmt=sql.query(conn_str,"SELECT * from TestTable");stmt.on('meta',function(meta){console.log("We've received the metadata");});stmt.on('row',function(idx){console.log("We've started receiving a row");});stmt.on('column',function(idx,data,more){console.log(idx+":"+data);});stmt.on('done',function(){console.log("All done!");});stmt.on('error',function(err){console.log("We had an error :-( "+err);});
This example uses events, but the driver also supports callbacks. Here's an example replicating the functionality above, but with callbacks. In this scenario, we are connecting to a SQL Server 2012 Express LocalDB instance:
// Query with explicit connectionvarsql=require('msnodesql');varconn_str="Driver={SQL Server Native Client 11.0};Server={(localdb)\\v11.0};Database={DBName};Trusted_Connection={Yes};";sql.open(conn_str,function(err,conn){if(err){console.log("Error opening the connection!");return;}conn.queryRaw("SELECT * FROM TestTable",function(err,results){if(err){console.log("Error running query!");return;}for(vari=0;i<results.rows.length;i++){console.log("0:"+results.rows[i][0]);}});});
Conclusion
This guide served to give you a quick introduction to the functionality of the msnodesql driver. The driver is currently in a preview state and will evolve, so the documentation will be created an updated as we progress.