Questions And Answers

More Tutorials

NodeJS MSSQL Intergration

Connecting with SQL via. mssql npm module

We will start with creating a simple node application with a basic structure and then connecting with local sql server database and performing some queries on that database.

Step 1:

Create a directory/folder by the name of project which you intent to create. Initialize a node application using npm init command which will create a package.json in current directory .

mkdir mySqlApp
//folder created
cd mwSqlApp
//change to newly created directory
npm init
//answer all the question ..
npm install
//This will complete quickly since we have not added any packages to our app.

Step 2:

Now we will create a App.js file in this directory and install some packages which we are going to need to connect to sql db.

sudo gedit App.js
//This will create App.js file , you can use your fav. text editor :)
npm install --save mssql
//This will install the mssql package to you app

Step 3:

Now we will add a basic configuration variable to our application which will be used by mssql module to establish a connection .

console.log("Hello world, This is an app to connect to sql server.");
var config = {
 "user": "myusername", //default is sa
 "password": "yourStrong(!)Password",
 "server": "localhost", // for local machine
 "database": "staging", // name of database
 "options": {
 "encrypt": true
sql.connect(config, err => {
 throw err ;
 console.log("Connection Successful !");
 new sql.Request().query('select 1 as number', (err, result) => {
 //handle err
 // This example uses callbacks strategy for getting results.
sql.on('error', err => {
 // ... error handler
 console.log("Sql database connection error " ,err);

Step 4:

This is the easiest step ,where we start the application and the application will connect to the sql server and
print out some simple results .

node App.js
// Output :
// Hello world, This is an app to connect to sql server.
// Connection Successful !
// 1


In this page (written and validated by ) you learned about NodeJS MSSQL Intergration . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS with Oracle.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.