我无法连接到SQL Server与mssql库为我的express项目。我可以连接VS Code和SQL Server Management Studio
var express = require('express');
var app =
express();
const sql = require('mssql')
var config = {
user: 'eren',
password: 'PAssPoxx1.',
server: 'localhost',
database: 'AIRPLANE',
options: {
encrypt: true,
enableArithAbort: true}
};
const poolPromise = new sql.ConnectionPool(config)
.connect()
.then(pool => {
console.log('Connected to MSSQL')
return pool
})
.catch(err => console.log('Database Connection Failed! Bad Config: ', err))
app.get('/', function (req, res) {
// Configuration object for your database
// connect to the database
sql.connect(config, function (err) {
console.log("x")
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from User', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
var server = app.listen(3000, function () {
console.log('Server is running..');
});
My errors显示:
数据库连接失败!错误配置:ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)在连接。(C: 用户ıl Eren 数据库api node_modules 下载该软件 lib 乏味 connection-pool.js: 68:17)在对象。onceWrapper (events.js 422:26):在连接。发出(events.js 315:20):在连接。socketError (C:UsersAnıl enDownloadsdatabase-apinode_modulesmssqlnode_modules libconnection.js:1290:12)
at C:UsersAnıl enDownloadsdatabase-apinode_modulesmssqlnode_modules乏味libconnection.js:1116:21在SequentialConnectionStrategy。connect (C:UsersAnıl even Downloadsdatabase-apinode_modulesmssqlnode_modules繁琐libconnector.js:87:14)在套接字。onError (C:UsersAnıl even Downloadsdatabase-apinode_modulesmssqlnode_modules繁琐libconnector.js:100:12)在套接字。发出(events.js 315:20):/streams/destroy.js:106:8at emitErrorCloseNT (internal/streams/destroy.js:74:3) {代码:"ESOCKET",originalError: ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)at ConnectionError (C:UsersAnıl even Downloadsdatabase-apinode_modulesmssqlnode_modules繁琐liberrors.js:13:12)在连接。socketError (C:UsersAnıl enDownloadsdatabase-apinode_modulesmssqlnode_modules libconnection.js:1290:56)
at C:UsersAnıl enDownloadsdatabase-apinode_modulesmssqlnode_modules乏味libconnection.js:1116:21在SequentialConnectionStrategy。connect (C:UsersAnıl even Downloadsdatabase-apinode_modulesmssqlnode_modules繁琐libconnector.js:87:14)在套接字。onError (C:UsersAnıl even Downloadsdatabase-apinode_modulesmssqlnode_modules繁琐libconnector.js:100:12)在套接字。发出(events.js 315:20):/streams/destroy.js:106:8/streams/destroy.js:74:3在processTicksAndRejections (internal/process/task_queues.js:80:21) {代码:"ESOCKET"}}
编辑:TCP/IP启用端口1433
var config = {
user: 'eren',
password: 'PAssPoxx1.',
server: 'localhost',
database: 'AIRPLANE',
options: {
encrypt: true,
enableArithAbort: true}
};
你的sqlserver实例是否命名为mssqlserver?如果没有,在大多数情况下必须指定实例名'localhostSQLEXPRESS'。