He probado de varias formas, entre ellas las que se muestran aquí:
https://www.microsoft.com/en-us/sql-server/developer-get-started/node/windows/step/2.html
(son más o menos lo mismo)
Y me da este error:
ConnectionError: Failed to connect to LAPTOP-xxxxxSQLEXPRESS:1433 - getaddrinfo ENOTFOUND LAPTOP-xxxxxSQLEXPRESS code: 'ESOCKET'
Mi código:
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
// Create connection to database
var config = {
server: 'LAPTOP-xxxxx\SQLEXPRESS',
authentication: {
type: 'default',
options: {
userName: 'xxxx', // update me
password: 'xxxx', // update me
}
},
options: {
database: 'SampleDB'
}
}
var connection = new Connection(config);
// Attempt to connect and execute queries if connection goes through
connection.on('connect', function(err) {
if (err) {
console.log(err);
} else {
console.log('Connected');
}
});
connection.connect();
También he cambiado el puerto TCP a 1433 para que coincida con el que está por defecto pero da el mismo error.
Otra forma:
const sql = require('mssql');
try {
sql.connect('mssql://xxxx:xxxx@LAPTOP-xxxx\SQLEXPRESS/GDP?encrypt=true')
console.log('Se ha conectado a la base de datos')
} catch (error) {
console.log(error)
}
export default sql;
Y su error:
tedious deprecated The default value for
config.options.enableArithAbort
will change fromfalse
totrue
in the next major version oftedious
. Set the value totrue
orfalse
explicitly to silence this message. node_modules\mssql\lib\tedious\connection-pool.js:61:23(node:31776) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:31776) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
server
no es correcto. Entiendo que la documentación no aporta mucho al respecto, pero para conectarte a una instancia con nombre debes añadirla como un campo llamadoinstanceName
dentro del campooptions
. Tal como se documenta aquí. Intenta eso y comenta el resultado. Saludosserver
aserver: 'LAPTOP-xxxxx'
y he añadido enoptions
:instanceName: 'SQLEXPRESS'
. El nuevo error ahora es: ConnectionError: Failed to connect to LAPTOP-xxxx\SQLEXPRESS in 15000ms. code: 'ETIMEOUT'.