简体   繁体   中英

SQL Server connection with Node js

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: '1234',
        server: 'AHMAD\SQLEXPRESS', 
        database: 'dbms_lab4',
        port:1433,
        encrypt:false
    };

    // 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 Products', 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..');
});

I can't connect to SQL Server. When I run the above program and access localhost:5000, nothing appears on the browser but a message on the terminal of vscode:

"The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. node_modules\mssql\lib\tedious\connection-pool.js:61:23
ConnectionError: Connection is closed.
    at Request._query (D:\JS\Sql_Connection\node_modules\mssql\lib\base\request.js:447:37)
    at Request._query (D:\JS\Sql_Connection\node_modules\mssql\lib\tedious\request.js:346:11)
    at Request.query (D:\JS\Sql_Connection\node_modules\mssql\lib\base\request.js:383:12)
    at Immediate.<anonymous> (D:\JS\Sql_Connection\server.js:27:17)
    at processImmediate (internal/timers.js:458:21) {
  code: 'ECONNCLOSED',
  name: 'ConnectionError'
}
ConnectionError: Failed to connect to AHMADSQLEXPRESS:1433 - getaddrinfo ENOTFOUND AHMADSQLEXPRESS
    at Connection.<anonymous> (D:\JS\Sql_Connection\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
    at Object.onceWrapper (events.js:428:26)
    at Connection.emit (events.js:321:20)
    at Connection.socketError (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connection.js:1290:12)
    at D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connection.js:1116:21
    at GetAddrInfoReqWrap.callback (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connector.js:158:16)
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:76:17) {
  code: 'ESOCKET',
  originalError: ConnectionError: Failed to connect to AHMADSQLEXPRESS:1433 - getaddrinfo ENOTFOUND AHMADSQLEXPRESS
      at ConnectionError (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\errors.js:13:12)
      at Connection.socketError (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connection.js:1290:56)
      at D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connection.js:1116:21
      at GetAddrInfoReqWrap.callback (D:\JS\Sql_Connection\node_modules\mssql\node_modules\tedious\lib\connector.js:158:16)
      at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:76:17) {
    message: 'Failed to connect to AHMADSQLEXPRESS:1433 - getaddrinfo ENOTFOUND AHMADSQLEXPRESS',
    code: 'ESOCKET'
  },
  name: 'ConnectionError'
}
ConnectionError: Connection is closed.
    at Request._query (D:\JS\Sql_Connection\node_modules\mssql\lib\base\request.js:447:37)
    at Request._query (D:\JS\Sql_Connection\node_modules\mssql\lib\tedious\request.js:346:11)
    at Request.query (D:\JS\Sql_Connection\node_modules\mssql\lib\base\request.js:383:12)
    at D:\JS\Sql_Connection\server.js:27:17
    at D:\JS\Sql_Connection\node_modules\mssql\lib\base\connection-pool.js:241:7
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 'ECONNCLOSED',
  name: 'ConnectionError'
}"

For SQL Connections: "Microsoft style strings of hostname\\instancename are not supported."

EG from : https://www.w3schools.com/nodejs/nodejs_mysql.asp

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

You need to update your config accordingly.

try escaping the \\ in the server name

Change

server: 'AHMAD\SQLEXPRESS'

To

server: 'AHMAD\\SQLEXPRESS', 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM