11

I am trying to establish a connection between nodejs project and server running Microsoft SQL Server 2005. I am using a node module mssql, but I get these errors when I am attempting to create a connection:

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

My connection being made by

var sql = require('mssql');

var dbConfig = {
    server:'123.123.12.1',
    database:'testingDB',
    user:'userName',
    password:'pass',
    port:1433
};

function getEmp() {
    var conn = new sql.Connection(dbConfig);
    var req = new sql.Request(conn);

    conn.connect(function(err) {
        if(err) {
            console.log(err);
            return;
        }
    else {
        console.log('success');
    }
});
}

getEmp();

I am not sure what I am doing wrong, I am using a cloud 9 IDE if that helps.

1

3 Answers 3

14

Put your var req = new sql.Request(conn) inside connect.

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

    });
});
Sign up to request clarification or add additional context in comments.

Comments

9

It work for me. First install mssql by npm (npm install --save mssql)

var sql = require('mssql');

for local sql server

var sqlConfig = {
  user: 'sa',
  password: 'admin',
  server: 'CBMOBILESHAMIM\\SQLEXPRESS',  
  database: 'databaseName'
};

for azure server

var sqlConfig = {
  user: 'adminLogin',
  password: 'admin',
  server: 'severname.database.windows.net',    // don't add tcp & port number
  database: 'databaseName',
  options: {
    encrypt: true
  }
};

Now connect to server

(async function () {
  try {
    console.log("sql connecting......")
    let pool = await sql.connect(sqlConfig)
    let result = await pool.request()
      .query('select * from Subject')  // subject is my database table name

    console.log(result )

  } catch (err) {
    console.log(err);
  }
})()

for more details check mssql

Comments

1
var webconfig = {

user: 'login',

password: 'sa@123',

server: 'localhost', 

database: 'TestDB',



options: {

    encrypt: false // Use this if you're on Windows Azure 

}

  }




 var express = require('express');

 var sql = require('mssql');

 var http = require('http');


var connection = new sql.Connection(webconfig, function(err) {
var request = new sql.Request(connection); 
request.query('select * from Users', function(err, recordset) {
   if(err)      // ... error checks 
        console.log('Database connection error');

console.dir("User Data: "+recordset);
});
 });

 var app = express();

 var port = process.env.PORT || 8000;

or visit here : https://nodejsbeginersprograms.blogspot.in/2017/02/nodejs-basic-tutorial-with-mssql.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.