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