Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Access SQL Server in Node - Js

This document explains how to access a SQL Server database from a Node.js application using the mssql driver. It installs the mssql driver, connects to a local SQL Server Express database, executes a query to retrieve records from a table, and displays the records on an Express server running on port 5000.

Uploaded by

bkk.prog2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Access SQL Server in Node - Js

This document explains how to access a SQL Server database from a Node.js application using the mssql driver. It installs the mssql driver, connects to a local SQL Server Express database, executes a query to retrieve records from a table, and displays the records on an Express server running on port 5000.

Uploaded by

bkk.prog2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/19/23, 3:03 PM Access SQL Server in Node.

js

 Previous Next 

Access SQL Server in Node.js


Learn how to access relational database MS SQL Server 2012 in Node.js application using Express.js in this section.

In order to access MS SQL database, we need to install drivers for it. There are many drivers available for SQL server in NPM. We will use mssql
driver here.

Install Driver
Install mssql driver using npm command, npm install mssql in the command prompt. This will add mssql module folder in node_modules folder in
your Node.js application. This tutorial uses mssql v2.3.1, which is latest version as of now.

After installing the driver, we are ready to access MS SQL server database. We will connect to a local SQLExpress database server and fetch all the
records from Student table in SchoolDB database shown below.

Database Table

Now, create server.js and write the following code.

Server.js  Copy

var express = require('express');


var app = express();

app.get('/', function (req, res) {

var sql = require("mssql");

// config for your database


var config = {
user: 'sa',
password: 'mypassword',
server: 'localhost',
database: 'SchoolDB'
};

// connect to your database


sql.connect(config, function (err) {

if (err) console.log(err);

// create Request object


var request = new sql.Request();

// query to the database and get the records


request.query('select * from Student', function (err, recordset) {

if (err) console.log(err)

// send records as a response


res.send(recordset);

});
});
});

var server = app.listen(5000, function () {


console.log('Server is running..');
});

In the above example, we have imported mssql module and called connect() method to connect with our SchoolDB database. We have passed config
object which includes database information such as userName, password, database server and database name. On successful connection with the
database, use sql.request object to execute query to any database table and fetch the records.

Run the above example using node server.js command and point your browser to http://localhost:5000 which displays an array of all students from
Student table.

https://www.tutorialsteacher.com/nodejs/access-sql-server-in-nodejs 1/3
9/19/23, 3:03 PM Access SQL Server in Node.js

Access SQL Server from Node.js

Thus, you can access MS SQL Server database and execute queries using mssql module. Visit npm documentation to learn more about mssql.

Learn how to access MongoDB in Node.js in the next section.

Want to check how much you know Node.js?

Start Node.js Test

 Share  Tweet  Share  Whatsapp

 Previous Next 

.NET Tutorials

C#

Object Oriented C#

ASP.NET Core

ASP.NET MVC

LINQ

Inversion of Control

Web API

Database Tutorials

SQL

SQL Server

PostgreSQL

MongoDB

JavaScript Tutorials

JavaScript

TypeScript

jQuery

Angular 11

Node.js

D3.js

Sass

Programming Tutorials

Python

Go lang

https://www.tutorialsteacher.com/nodejs/access-sql-server-in-nodejs 2/3
9/19/23, 3:03 PM Access SQL Server in Node.js

HTTPS (SSL)

TutorialsTeacher.com E-mail list

TutorialsTeacher.com is optimized for learning web Subscribe to TutorialsTeacher email list and get latest updates, tips & tricks on C#, .Net, JavaScript, jQuery, AngularJS,
technologies step by step. Examples might be simplified to Node.js to your inbox.

improve reading and basic understanding. While using this

site, you agree to have read and accepted our terms of Email address GO
use and privacy policy.
We respect your privacy.

 2023 TutorialsTeacher.com. All Rights Reserved.


 Contact Us

HOME TERMS OF USE PRIVACY POLICY  2023 TutorialsTeacher.com. All Rights Reserved.

https://www.tutorialsteacher.com/nodejs/access-sql-server-in-nodejs 3/3

You might also like