0

在尝试查看几十个示例并阅读大多数文档并尝试许多不同变体并更改SQL Server中的许多设置后,终于崩溃了寻求这个帮助。使用knexjs连接到SQL Server,无法获取连接

我使用与SQL Server接受的完全相同的连接字符串成功连接到带有mssqljs的tblTextKnex,但一段时间以来一直无法使用knexjs。

我得到以下警告和错误:

Knex:warning - calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.

Unhandled rejection Error: Unable to acquire a connection

这是我认为应该工作不成功的/有问题的代码。

var knex = require('knex')({ 
    client: 'mssql', 
    connectionString: "Initial Catalog=TextKnex;Data Source=localhost\\TESTINSTANCE;User ID=my_user_id;Password=my_password;Integrated Security=SSPI;" 
    }); 

    knex().connection().then(() => { 
    knex('TextKnex').table('Products') 
     .select('Products.Price as Price') 
     .then((product) => { 
      console.log('log product', product); 
      console.dir('dir product', product); 
      logger.info('Query Data: %j', product); 
    }) 
    }); 
    knex.destroy(); 

回答

1

我敢肯定,没有connectionString属性和connection()查询生成器方法记录在案,以无法正常工作(而不是检查,如果池已连接)。最后同步调用knex.destroy()最终破坏您的knex实例,在进行任何查询或连接之前。

试试这个:

var knex = require('knex')({ 
    client: 'mssql', 
    connection: { 
     connectionString: "Initial Catalog=TextKnex;Data Source=localhost\\TESTINSTANCE;User ID=my_user_id;Password=my_password;Integrated Security=SSPI;" 
    } 
    }); 

    knex('TextKnex').table('Products') 
    .select('Products.Price as Price') 
    .then((product) => { 
     console.log('log product', product); 
     console.dir('dir product', product); 
     logger.info('Query Data: %j', product); 
    }) 
    .finally(() => { 
     knex.destroy(); 
    }); 

var knex = require('knex')({ 
    client: 'mssql', 
    connection: "Initial Catalog=TextKnex;Data Source=localhost\\TESTINSTANCE;User ID=my_user_id;Password=my_password;Integrated Security=SSPI;" 
    }); 

    ... 

在knex测试MSSQL的连接做了一些不同的方式:https://github.com/tgriesser/knex/blob/master/test/knexfile.js#L132

+0

既不是第一个也不是第二个选项为您提供工作,这是考虑到knex.org的状态令人惊讶“连接选项直接传递给适当的数据库客户端来创建连接,并且可能是对象或连接字符串:“ – Urasquirrel

+0

我也尝试将版本从4更改为3.3.0,因为这也为其他人带来了问题。这也不起作用, – Urasquirrel

+0

也在测试文件中使用建议的格式不起作用。 knex =要求( 'knex')({ 方言: 'MSSQL', 连接:{ 用户: “计算机\\ kyles2”, 密码: “p_w”, 服务器: “127.0.0.1”, 数据库: “TextKnex”, port:59438 } – Urasquirrel