问题
I am using KnexJs attempting to connect to a local Microsoft SQL Server Express. However, with the below configuration, I am getting an error. I've followed the typical steps, but I'm still getting the error.
What I've tried:
- Set up a SQL Server authentication login for the database
- Enable SQL Server authentication on the server
- Enable TCP/IP on the server
- Restart the Windows services
- Restart the SQL Server through SQL Server Management Studio
- Verify ability to log in through SQL Server Management Studio
Configuration / query code:
let mssql = knex({
client: 'mssql',
connection: {
host: 'localhost\\sqlexpress',
user: 'test',
password: 'test',
database: 'AdventureWorks2017',
// port:1433,
// options: {
// trustedConnection: true
// },
useNullAsDefault: true
}
});
mssql.raw('select 1 as result').then(function (result) {
console.log('result');
console.log(result);
mainWindow.webContents.send('testConnectionResponse', result === 1);
event.sender.send('testConnectionResponse', result === 1);
}).catch(function (err) {
console.log(err);
mainWindow.webContents.send('query-error', err);
}).finally(() => {
mssql.destroy();
});
Error:
ConnectionError: Failed to connect to localhost:undefined in 15000ms
回答1:
It turns out that I also needed to enable the SQL Server Browser windows service like so:
- Navigate to "Services"
- Select "Properties" on "SQL Server Browser"
- Flip "Start up type" to "Automatic"
- Start the service
Success!
来源:https://stackoverflow.com/questions/49185969/connectionerror-failed-to-connect-to-localhostundefined-in-15000ms