Node-mssql: I dont connect

Created on 8 Jun 2015  路  12Comments  路  Source: tediousjs/node-mssql

Allways the next error with the example code:

{ [ConnectionError: Connection is closed.]
name: 'ConnectionError',
message: 'Connection is closed.',
code: 'ECONNCLOSED' }

Most helpful comment

Also - if your connecting to localhost\instance make sure you have the service 'SQL Server Browser' running.

All 12 comments

Please, post the exact code you're using.

I just start to learn Node and I need to make an API Rest with MS SQL Server.

For test I install SQL Express 2014 (PC01\SQLEXPRESS) and Ms SQL Server Management Studio in my Windows 8.1 PC. I create a database "db01" with a table named "Users":

To start my test connection I run this code in node:

var sql = require('mssql'); 

var config = {
    user: 'sa',
    password: '1234',
    server: 'localhost\\SQLEXPRESS', // You can use 'localhost\\instance' to connect to named instance 
    database: 'db01',
    stream: true,
    options: {
      encrypt: true
    }// Use this if you're on Windows Azure
}

console.log(config.server);

sql.connect(config, function(err) {
    // ... error checks

    var request = new sql.Request();
    request.stream = true; // You can set streaming differently for each request
    request.query('select * from users'); // or request.execute(procedure);

    request.on('recordset', function(columns) {
        // Emitted once for each recordset in a query
        console.log(columns);
    });

    request.on('row', function(row) {
        // Emitted for each row in a recordset
        console.log(row);
    });

    request.on('error', function(err) {
        // May be emitted multiple times
        console.log(err);
    });

    request.on('done', function(returnValue) {
        // Always emitted as the last one
        console.log(returnValue);
    });
});

And the result:

C:\Users\juanjo\nodesql>node server.js
localhost\SQLEXPRESS
{ [ConnectionError: Connection is closed.]
  name: 'ConnectionError',
  message: 'Connection is closed.',
  code: 'ECONNCLOSED' }
undefined

Thanks for your help. Probably I need a basic step by step for this, a tutorial or basic working code but I don't find anything about this.

Another test:

var sql = require('mssql'); 

var config = {
    user: 'sa',
    password: '1234',
    server: 'localhost\\SQLEXPRESS', // You can use 'localhost\\instance' to connect to named instance 
    database: 'db01',
     options: {
      encrypt: true
    }// Use this if you're on Windows Azure
} 
sql.connect(config, function(err) {
    // ... error checks 

    // Query 

    var request = new sql.Request();
    request.query('select 1 as number', function(err, recordset) {
        // ... error checks 
        console.log(err); 
        console.dir(recordset);
    });
});

And the result:

C:\Users\juanjo\nodesql>node server.js
localhost\SQLEXPRESS
{ [ConnectionError: Connection is closed.]
  name: 'ConnectionError',
  message: 'Connection is closed.',
  code: 'ECONNCLOSED' }
undefined

You're missing connection error handler, I believe this is a place where problem originates.

var sql = require('mssql'); 

var config = {
    user: 'sa',
    password: '1234',
    server: 'localhost\\SQLEXPRESS', // You can use 'localhost\\instance' to connect to named instance 
    database: 'db01',
     options: {
      encrypt: true
    }// Use this if you're on Windows Azure
} 
sql.connect(config, function(err) {
    // ... error checks 
    console.log(err); 
    if (err) return;

    // Query 

    var request = new sql.Request();
    request.query('select 1 as number', function(err, recordset) {
        // ... error checks 
        console.log(err); 
        console.dir(recordset);
    });
});

With your code diferent message:

{ [ConnectionError: Failed to connect to localhost:undefined in 15000ms]
  name: 'ConnectionError',
  message: 'Failed to connect to localhost:undefined in 15000ms',
  code: 'ETIMEOUT' }

TCP protocol is not enabled on your server. Please see https://msdn.microsoft.com/en-GB/library/bb909712(v=vs.90).aspx

@jjgonver I had the same problem and I fix it only writing "localhost" in the server field

var dbConfig = {
server: "localhost",
database: "casa_Tornillo",
user: "sa",
password: "root",
port:"1433"
};

Also - if your connecting to localhost\instance make sure you have the service 'SQL Server Browser' running.

@bundyfx Godmode. You saved me hours. Thanks.

@bundyfx Thx a lot...working on it for hours...Not sure why the 'SQL Server Browser' service stopped...

@mario-galindo suggestion worked for me. I was able to connect to the named instance 'SQLEXPRESS' by only setting sever: 'localhost'.

I also had a typo in the database name which was not gracefully reported by tedious. I only realized when connecting using .NET SqlClient.

Configuration:
var config = {
user: 'sa',
password: '**',
server: 'localhost\MSSQLSERVERDEV', // You can use 'localhost\instance' to connect to named instance
database: 'dbname',

options: {
    trustedConnection: true,
    encrypt: false // Use this if you're on Windows Azure
},
port:57350,
};

Things to remember:

  1. Enable TCP/IP
    image
  2. Check Sql Browser Service running.

that's it . Keep coding. 馃挴

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agilitehub picture agilitehub  路  4Comments

jeetendra-choudhary picture jeetendra-choudhary  路  3Comments

sizovilya picture sizovilya  路  3Comments

brilvio picture brilvio  路  3Comments

linvi picture linvi  路  3Comments