tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.22k stars 464 forks source link

How to connect to SQL Server using tedious (Windows Authentication) #1606

Closed marek8623 closed 4 months ago

marek8623 commented 4 months ago

I am trying to get data from local SQL Server database.

Here is my code:

const sql = require("mssql");

const config = {
    server: 'localhost\\SQLEXPRESS',
    database: 'LocalDB',
    options: {
        trustServerCertificate: true
    }
};

function getData() {
    const connection = new sql.ConnectionPool(config);
    const request = new sql.Request(connection);

    connection.connect(function (error) {
        if (error) {
            console.log(error);
            return;
        }
        request.query("SELECT * FROM Table_Name", function (error, recordset) {
            if (error) {
                console.log(error);
            } else {
                console.log(JSON.stringify(recordset));
            }
            connection.close();
        });
    });
}

getData();

I want to use Windows Authentication login so that I do not have to provide username and password.

If I used msnodesqlv8 driver I was able to connect like this:

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

const config = {
    driver: 'msnodesqlv8',
    connectionString: 'Driver={ODBC Driver 17 for SQL Server};Server=localhost\\SQLEXPRESS;Database=LocalDB;Trusted_Connection=yes;'
};

Expected behaviour:

I want to use built-in tedious driver but I do not know how to do it properly. Can someone help me what I am missing in the config? Thanks

Actual behaviour:

Using tedious driver and first mentioned config I get error: ConnectionError: Login failed for user ''

Software versions

dhensby commented 4 months ago

The tedious driver isn't really "built in" it's still an external dependency (it's just one that's always installed).

You'll need to consult the tedious docs for how to authenticate with windows authentication.