How to connect to SQL Server with windows authentication from Node.JS using mssql module

前端 未结 12 1404
忘掉有多难
忘掉有多难 2020-11-27 13:57

Hi I\'m unable to connect to SQL server that is using windows authentication in node js. I\'m using the mssql module. The error message is :

[ConnectionError         


        
12条回答
  •  广开言路
    2020-11-27 14:27

    I have never been able to get mssql + windows auth to work for any of my projects. Try edge and edge-sql - it has worked for me. Be sure you install all the required packages.

    https://github.com/tjanczuk/edge

    https://github.com/tjanczuk/edge-sql

    From there, it's pretty steamlined.

    var edge = require('edge');
    var params = {
      connectionString: "Server=YourServer;Database=YourDB;Integrated Security=True",
      source: "SELECT TOP 20 * FROM SampleData"
    };  
    var getData = edge.func( 'sql', params);
    
    getData(null, function (error, result) {
       if (error) { console.log(error); return; }
       if (result) {
        console.log(result);
       }
       else {
        console.log("No results");
       }
     });
    

    EDIT

    Well... 10 days after my original answer, apparently mssql added Windows Auth to the package. They heard our cries :) See here. I have not tested it yet, but it is officially in my backlog to test integration. I will report back.

    FWTW, if mssql fits your needs, I would go with it, as 1) edge-sql has been dormant for 2 years and 2) the primary contributor has said he has left projects like this "in the caring hands of Microsoft", since he no longer works there.

    EDIT 2

    This keeps getting upvotes and there are comments saying some of the other answers' code examples either aren't working or aren't working on Windows.

    This is my code using mssql, working on Windows, with msnodesqlv8 also installed:

    var sql = require('mssql/msnodesqlv8');
    var config = {
      driver: 'msnodesqlv8',
      connectionString: 'Driver={SQL Server Native Client XX.0};Server={SERVER\\NAME};Database={dbName};Trusted_Connection={yes};',
    };
    
    sql.connect(config)
    .then(function() {
     ...profit...
    })
    .catch(function(err) {
      // ... connect error checks
    });
    

提交回复
热议问题