Creatos
Creatos

Reputation: 1

Connecting SQL Server to nodejs

My current js node code:

const sql = require('mssql')
const sqlConfig = {
  user: 'sa',
  password: '12345678',
  database: 'mementoDB',
  server: 'LAPTOPSZKOLA\\SQLEXPRESS',
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 30000
  },
  options: {
    encrypt: false,
    trustServerCertificate: true
  }
}

const func = async () => {
 try {
  await sql.connect(sqlConfig);
  const result = await sql.query(`select * from People`);
  console.dir(result);
 } catch (err) {
  console.log(err);
 }
}
func();

My error:

ConnectionError: Failed to connect to LAPTOPSZKOLA\SQLEXPRESS in 15000ms at C:\Users\creat\OneDrive\Pulpit\jsapps\tut2\node_modules\mssql\lib\tedious\connection-pool.js:85:17 at Connection.onConnect (C:\Users\creat\OneDrive\Pulpit\jsapps\tut2\node_modules\tedious\lib\connection.js:838:9) at Object.onceWrapper (node:events:629:26) at Connection.emit (node:events:514:28) at Connection.emit (C:\Users\creat\OneDrive\Pulpit\jsapps\tut2\node_modules\tedious\lib\connection.js:959:18) at Connection.connectTimeout (C:\Users\creat\OneDrive\Pulpit\jsapps\tut2\node_modules\tedious\lib\connection.js:1209:10) at Timeout._onTimeout (C:\Users\creat\OneDrive\Pulpit\jsapps\tut2\node_modules\tedious\lib\connection.js:1154:12) at listOnTimeout (node:internal/timers:573:17) at process.processTimers (node:internal/timers:514:7) { code: 'ETIMEOUT', originalError: ConnectionError: Failed to connect to LAPTOPSZKOLA\SQLEXPRESS in 15000ms at Connection.connectTimeout (C:\Users\creat\OneDrive\Pulpit\jsapps\tut2\node_modules\tedious\lib\connection.js:1209:26) at Timeout._onTimeout (C:\Users\creat\OneDrive\Pulpit\jsapps\tut2\node_modules\tedious\lib\connection.js:1154:12) at listOnTimeout (node:internal/timers:573:17) at process.processTimers (node:internal/timers:514:7) { code: 'ETIMEOUT' } }

My SQL Server:

db structure and working query

Upvotes: 0

Views: 166

Answers (2)

Creatos
Creatos

Reputation: 1

I've got it!! my "SQL Server Browser" service wasn't running. If you are having similar issue you can run this service in "Sql Server Configuration Manager"

Upvotes: 0

Arqam Rafay
Arqam Rafay

Reputation: 135

".\SQLEXPRESS" or "localhost"

Find out host name by using SELECT @@SERVERNAME

Upvotes: 0

Related Questions