Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to SQL Server from my VS Code extension (node.js)

I'm developing VS Code extension and want to get some data from SQL Server database. I've tried many different examples but non of them are working for me.

        const Connection = require('tedious').Connection;

        const config = {
            user: 'ssrs', // tried userName, username
            password: 'ssrs', 
            server: 'localhost',
            options: {
                database: 'imdb' 
            }
          }

          const connection = new Connection(config);
          connection.on('connect', function(err: string) {
            if (err) {
              console.log(err);
            } else {
              console.log('Connected');
            }
          });

OR

        await sql.connect('mssql://ssrs:ssrs@localhost/imdb')
        const result = await sql.query`select 1 as one`
        console.dir(result)

OR

    const sql = require("mssql");
    const conn = new sql.ConnectionPool({
        database: "imdb",
        server: "localhost",
        driver: "msnodesqlv8",
        userName: "ssrs",
        password: "ssrs"
    });

    conn.connect().then(() => {
        console.log('Connected');
    });

ConnectionError: Login failed for user ''.

enter image description here

Connecting via sqlcmd:

enter image description here

The port is also open (1433) as I can telnet to it.

Messages from SQL Server log:

Date 2019-05-08 11:19:02 AM Log SQL Server (Current - 2019-05-05 2:53:00 PM)

Source Logon

Message Login failed for user ''. Reason: An attempt to login using SQL authentication failed. Server is configured for Integrated authentication only. [CLIENT: 127.0.0.1]

Date 2019-05-08 11:19:02 AM Log SQL Server (Current - 2019-05-05 2:53:00 PM)

Source Logon

Message Error: 18456, Severity: 14, State: 58.

Trying to connect with Powershell:

  PS C:\WINDOWS\system32> Invoke-Sqlcmd -query "select CURRENT_USER, USER_NAME()" -ServerInstance "localhost" -username "ssrs" -password "ssrs" -Database 'imdb'

Column1 Column2
------- -------
ssrs    ssrs   
like image 274
Dmitrij Kultasev Avatar asked May 05 '19 12:05

Dmitrij Kultasev


People also ask

How do I connect to Vscode in SQL Server?

In Visual Studio Code, press Ctrl+Shift+P (or F1) to open the Command Palette. Select MS SQL:Connect and choose Enter. Select Create Connection Profile. Follow the prompts to specify the new profile's connection properties.

How do I connect to node js in SQL Server?

Install Driver. Install mssql driver using npm command, npm install mssql in the command prompt. This will add mssql module folder in node_modules folder in your Node. js application.

How do I connect Visual Studio to SQL Server Management Studio?

Right click on 'Data Connections' then click 'Add Connection'. On the next screen (Choose Data Source) change the data source ensure 'Microsoft SQL Server' is selected then click the 'Continue' button. In the 'Server name' field enter 'SQL-SERVER'. Change the 'Authentication' method to 'SQL Server Authentication'.


1 Answers

As shown in your screenshot, you're able to connect via SQLCMD utility. This means there's no issue with the user or the authentication mode in SQL Server. The error message is however misleading and other users already experienced this here.

I'm no expert of node.js but I'd recommend you to better understand how the mssql plugin and Connection object work. I think that some missing setting in your configuration is making the connection attempt fail.

For example, this code you posted is wrong (userName is not valid):

const sql = require("mssql");
const conn = new sql.ConnectionPool({
    database: "imdb",
    server: "localhost",
    driver: "msnodesqlv8",
    userName: "ssrs",
    password: "ssrs"
});

conn.connect().then(() => {
    console.log('Connected');
});

It should be:

const sql = require("mssql");
const conn = new sql.ConnectionPool({
    database: "imdb",
    server: "localhost",
    driver: "msnodesqlv8",
    user: "ssrs",
    password: "ssrs"
});

conn.connect().then(() => {
    console.log('Connected');
});
like image 177
Alberto Solano Avatar answered Oct 06 '22 01:10

Alberto Solano