2
votes

I'm trying to connect to a SQL Server Express database setup on a Windows 10 machine in network from a macbook running the following code. I can connect using tedious, but not using mssql. It's as if the username or password is wrong. The reason I want to get mssql working is for the pooling and because it appears to have more support... otherwise I would just use tedious. The code is creating both connections and the output shows tedious connecting but mssql fails.

const express = require('express');
const sql = require('mssql');
const bodyParser = require('body-parser');
const app = express();

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

// Body Parser Middleware
app.use(bodyParser.json());

// CORS Middleware
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

// define listineng port
const PORT = 5006;

// define tedious config
var config = {
            server: "JAMBER-VR1.local",
            userName: "testuser",
            password: "testuserpass",
            database: "SQLEXPRESS",
            options: {
              encrypt: false
            }
     };

var connection = new Connection (config);

connection.on('connect', function(err){

    if(err!=null){
      console.log(err);
         console.log("not connected - tedious");
    }
    else{
          console.log("Connected - tedious")
          connection.close();
    };
});


// Define SQL config

const dbConfig = {
  user: 'testuser',
  password: 'testuserpass',
  server: 'JAMBER-VR1.local',
  database: 'SQLEXPRESS',
  // port: 1433,
  // logging: true,
  options: {encrypt: false},
  pool: {
    max: 100,
    min: 0,
    idleTimeoutMillis: 30000
  }
}

const executeQuery = async () => {
  try {
    let pool = await sql.connect(dbConfig);
    console.log("pool connected - mssql");
    let result = await pool.request()
      .input('input_parameter', sql.Int, value)
      .query('select * from donor')

    console.log("Result: ", result);
  } catch (err) {
    console.log("Error: ", err);
  }
}

executeQuery();

I'm getting the following error.

[nodemon] starting node index.js
Running on port 5006
Connected - tedious
Error: { ConnectionError: Login failed for user 'testuser'.

at Connection.tedious.once.err (/Users/jordanszymczyk/Code/TRTL/server_sql /node_modules/mssql/lib/tedious.js:237:17)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13) at Connection.processLogin7Response (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/connection.js:1292:16) at Connection.message (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/connection.js:1805:14) at Connection.dispatchEvent (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/connection.js:1004:38) at MessageIO. (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/connection.js:884:18) at MessageIO.emit (events.js:182:13) at ReadablePacketStream. (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/message-io.js:104:16) at ReadablePacketStream.emit (events.js:182:13) code: 'ELOGIN', originalError: { ConnectionError: Login failed for user 'testuser'. at ConnectionError (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/errors.js:12:12) at Parser. (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/connection.js:628:33) at Parser.emit (events.js:182:13) at Parser. (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/token/token-stream-parser.js:54:15) at Parser.emit (events.js:182:13) at addChunk (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/readable-stream/lib/_stream_readable.js:291:12) at readableAddChunk (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/readable-stream/lib/_stream_readable.js:278:11) at Parser.Readable.push (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/readable-stream/lib/_stream_readable.js:245:10) at Parser.Transform.push (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/readable-stream/lib/_stream_transform.js:148:32) at doneParsing (/Users/jordanszymczyk/Code/TRTL/server_sql/node_modules/tedious/lib/token/stream-parser.js:110:18) message: 'Login failed for user \'testuser\'.', code: 'ELOGIN' }, name: 'ConnectionError' }

You can see the console.log("Connected - tedious") prints, then the mssql error follows immediately. I've also tried using the local IP with the same results. Testing through sqlcmd works.

3

3 Answers

2
votes

SQLEXPRESS will be the name of your SQL Server Express server instance (ie- Server). I don't see it in your code, but I'm guessing the database name is not SQLEXPRESS?

In your mssql connection dbConfig try changing Database to the real name of the database you created on your SQL Express instance.

3
votes

dont forget to active both authentication

enter image description here

0
votes

The fixed dgconfig is as follows:

const dbConfig = {
  user: 'testuser',
  password: 'testuserpass',
  server: 'JAMBER-VR1.local',
  database: 'donor',
  options: {encrypt: false},
  pool: {
    max: 100,
    min: 0,
    idleTimeoutMillis: 30000
  }
}

Removed SQLEXPRESS and replaced it with the database name.