ConnectionError:无法连接到 localhost:15000 毫秒内未定义

2022-01-10 00:00:00 node.js electron sql-server knex.js

我正在使用 KnexJs 尝试连接到本地 Microsoft SQL Server Express.但是,使用以下配置,我收到错误消息.我已按照典型步骤进行操作,但仍然出现错误.

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.

我尝试过的:

  1. 为数据库设置 SQL Server 身份验证登录
  2. 在服务器上启用 SQL Server 身份验证
  3. 在服务器上启用 TCP/IP
  4. 重启 Windows 服务
  5. 通过 SQL Server Management Studio 重启 SQL Server
  6. 验证通过 SQL Server Management Studio 登录的能力

配置/查询代码:

    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();
    });

错误:

ConnectionError: 无法连接到 localhost:undefined in 15000ms

ConnectionError: Failed to connect to localhost:undefined in 15000ms

推荐答案

原来我还需要像这样启用SQL Server Browser windows服务:

It turns out that I also needed to enable the SQL Server Browser windows service like so:

  1. 导航到服务"
  2. 在SQL Server Browser"上选择属性"
  3. 将启动类型"改为自动"
  4. 启动服务

成功了!

相关文章