Connect to SQL Server database from Node.js

后端 未结 6 1228
北荒
北荒 2020-12-04 14:46

The question duplicates some older questions, but the things may have changed since then.

Is there some official support for connecting to SQL Server from Node.js (e

6条回答
  •  隐瞒了意图╮
    2020-12-04 15:34

    //start the program
    var express = require('express');
    var app = express();
    
    app.get('/', function (req, res) {
    
        var sql = require("mssql");
    
        // config for your database
        var config = {
            user: 'datapullman',
            password: 'system',
            server: 'localhost', 
            database: 'chat6' 
        };
    
        // 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 emp", 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..');
    });
    

    //create a table as emp in a database (i have created as chat6)

    // programs ends here
    

    //save it as app.js and run as node app.js //open in you browser as localhost:5000

提交回复
热议问题