How To Connect SqlServer with Node.js In AWS(Amazon Services) -Part1

This proof of concept is created to check how node.js works with sqlserver database.

There are many drivers available to support sqlserver in node js. I have created this poc with most usable driver “MSSQL” and you can check available drivers by executing below command in nodejs environment.

Command: npm find sqlserver

Above command gives all available sqlserver drivier for node.js.

There are following steps to configure node.js with sqlserver and you have to create same steps in your machine to run this sample.

Step 1:  install node js package for sqlserver by using below command:

“Npm install mssql “

Step2: Create Sa login in sqlserver and also create database.here I have create sample database with name “EmpDb” with table name “Employees” with some records.

step2 step2-2 step2-3

Step3: Create below code in node.js application:

var sql = require(‘mssql’);

var config = {

user: “sa”,

password: “password@123”,

server: “Localhost”, // it can be servername if connecting directly with sqlserver or can be replace by RDS end point

example: ‘mydbinstance.abcdefghijkl.us-west-2.rds.amazonaws.com;dbname= EmpDb

database: “EmpDb”

}

function GetEmp() {

console.log(“step1”);

var connection = new sql.Connection(config);

var request = new sql.Request(connection); // or: var request = connection.request();

console.log(“step1 end”);

connection.connect(function (err) {

if (err) {

console.log(err);

return;

}

request.query(“select * from Employees”, function (err, recordset) {

if (err) {

console.log(err);

}

else {

console.log(recordset);

}

connection.close();

});

});

}

GetEmp();

Step4: Now run the application and below output well come:

step4

Note: I have used only select statement in this sample,If you want to perform update and delete then you can also use these operations.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s