I am trying to connect to MSSQL 2012 using NodeJS with the mssql connection interface.
When attempting to connect I get the following error:
{ [Conn
My issue was that I needed to start sqlserver using docker first on my mac using this command
sudo docker start sqlserver
I couldn't connect with 'localhost' although I use 'localhost' in SQL Management Studio and other applications. When I used Computer Name (network address), it worked!
**Please follow the connection configuration and little test:**
//Declare global variable
var http = require('http');
var events = require('events');
var nodemailer = require('nodemailer');
var sql = require('mssql');<br/>
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
//Create an http server
http.createServer(function(req,res)
{
res.writeHead(200, {'Content-Type': 'text/html'});
var Connection = require('tedious').Connection;
//Configure the connection
var config = {
userName: '<user id>',
password: '<password>',
server: '<system ip>',
options: {database: '<database name>'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
console.log("Connected");
executeStatement();
});
function executeStatement() {
request = new Request("select getdate();", function(err) {
if (err) {
console.log(err);}
});
var result = "";
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
connection.execSql(request);
};
return res.end();
}).listen(8080);
//Post configuration test on browser: http://localhost:8080/
If after enabling the TCP connection and still your configuration is not working. Here's my own-configuration.
var config = {
"user": 'admin',
"password": 'password',
"server": 'ALBERT-PC',
"database": 'database_name',
"port": '61427',
"dialect": "mssql",
"dialectOptions": {
"instanceName": "SQLEXPRESS"
}
};