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

前端 未结 12 1388
忘掉有多难
忘掉有多难 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:39

    I've tried many variations and this is my complete solution.
    I'm using SQL server Express.
    I'm connecting, in the first instance, to the MASTER database only.
    You only NEED to change "YOURINSTANCE\\SQLEXPRESS".
    (Be sure to maintain the double-slash above!!!)
    I'm using INTEGRATED SECURITY too.
    The query relies on nothing at all (in your database).
    You need to add your node packages
      ==> NPM INSTALL MSSQL and
      ==> NPM INSTALL msnodesqlv8
    Hopefully, your connection issues will be a thing of the past.
    Maybe.
    Please.

    // More here -> https://www.npmjs.com/package/mssql
    var sql = require('mssql/msnodesqlv8');
    var config = {
      connectionString: 'Driver=SQL Server;Server=YOURINSTANCE\\SQLEXPRESS;Database=master;Trusted_Connection=true;'
    };
    sql.connect(config, err => {
      new sql.Request().query('SELECT 1 AS justAnumber', (err, result) => {
        console.log(".:The Good Place:.");
        if(err) { // SQL error, but connection OK.
          console.log("  Shirtballs: "+ err);
        } else { // All is rosey in your garden.
          console.dir(result);
        };
      });
    });
    sql.on('error', err => { // Connection borked.
      console.log(".:The Bad Place:.");
      console.log("  Fork: "+ err);
    });
    

提交回复
热议问题