Skip to content

Node JS MSSQL

Sandesh Kota edited this page Nov 27, 2019 · 1 revision
  • To connect to MSSQL Database through Node.js we need to have MSSQL driver. First we need to ensure MSSQL is installed and running. Then we need to isntall the driver as below var sql = require("mssql")

  • Connection

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);
            
        });
    });

Clone this wiki locally