Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Connection with SQL Server windows authentication

I'm trying to connect node.js to mssql in Windows Authentication mode. I installed the tedious,mssql and msnodesqlv8 modules, but I still can't figure out how to open a connection.

This is what I tried:

    var sql = require('mssql');
    var config = {
    driver: 'msnodesqlv8',
    server: 'POPPY-GI\\SQLEXPRESS',
    database: 'NodinSports',
    options:{
    trustedConnection: true,
    useUTC: true}}

    sql.connect(config).then(function() { 
    new sql.Request().query('select * from users')
    .then(function(recordset){
    console.log(recordset);
    }).catch(function(err) {
    console.log(err);});
    }).catch(function(err) {
    console.log(err);});

After running I get a long error saying:

    `ConnectionError`: Port for `SQLEXPRESS` not found in 
 `ServerName`;POPPYGI;`InstanceName;SQLEXPRESS;IsClustered`;No;Version;12.0.2000.8;;

    at Connection.tedious.once.err (D:\Never Lazy\University\`AN2, SEM 2\WEB\`Projek`\node_modules\`mssql`\lib\`tedious.js:216:17`)

    at Connection.g (events.js:291:16)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:188:7)
    at D:\Never Lazy\University\AN2,SEM2\WEB\Projekt\node_modules\tedious\lib\connection.js:570:27
    at D:\Never Lazy\University\AN2,SEM2\WEB\Projekt\node_modules\tedious\lib\instance-lookup.js:91:24
    at Socket.onMessage (D:\Never Lazy\University\AN2,SEM2\WEB\Projekt\node_modules\tedious\lib\sender.js:140:9)
    at emitTwo (events.js:106:13)
    at Socket.emit (events.js:191:7)
    at UDP.onMessage (dgram.js:549:8)
    code: 'EINSTLOOKUP',

I would be really thankful for any help.

FIXED:

In services.msc check if the followings are enabled:

SQL Server(*server_name*) -- in my case `SQLEXPRESS`
SQL Server Browser
SQL Server Agent(*server_name*) -- if you are using `SQLEXPRESS` this doesn't need to run

In SQL Server Configuration Manager -> Protocols for server_name: enable TCP/IP.

To make sure everything will be fine, check the port the server is using (SQL Server Configuration Manager -> SQL Native Client Configuration -> Client Protocols -> double click on TCP/IP -> Default Port ) , and add the port: *your_port* to the code in var config = { ... }.

Lastly, change var sql = require('mssql'); to var sql = require("mssql/msnodesqlv8");

like image 679
Brigitta Avatar asked Nov 07 '22 20:11

Brigitta


1 Answers

Install the following modules:

"dependencies": {
    "msnodesqlv8": "^0.4.14",
    "mssql": "^4.1.0"
  }

My node version: v8.1.4

const sql = require("mssql/msnodesqlv8");

const main = async () => {
  const pool = new sql.ConnectionPool({
    server: "myservername",
    database: "mydbname",
    options: {
      trustedConnection: true
    }
  });

  await pool.connect();

  const request = new sql.Request(pool);

  const query = `SELECT [FirstName]
    ,[LastName]
    ,[Email]
FROM [Dev].[Users]`;

  const result = await request.query(query);

  console.dir(result);
};
main();

(You can do it without async or older versions: https://stackoverflow.com/a/40406998/639575)

like image 61
beatoss Avatar answered Nov 14 '22 22:11

beatoss