如何使用mssql模块从Node.JS通过windows身份validation连接到SQL Server

您好,我无法连接到在js节点使用Windows身份validation的SQL服务器。 我正在使用mssql模块。 错误消息是:

[ConnectionError: Login failed for user ''. The user is not associated with a trusted SQL Server connection.] name: 'ConnectionError', message: 'Login failed for user \'\'. The user is not associated with a trusted SQL Server connection.', code: 'ELOGIN' } 

这是我的代码:

 config = { server : "localhost\\MSSQLSERVER", database : "mydatabase", port : 1433 } function loadDepts() { var conn = new sql.Connection(config); var request = sql.Request(conn); conn.connect(function(err) { if (err) { console.log(err); return; } request.query("select deptid, deptname from departments", function(err, table) { if (err) { console.log(err); return; } else { console.log(table); } conn.close(); }); }); } loadDepts(); 

我从来没有能够获得mssql + Windowsvalidation我的任何项目工作。 尝试edgeedge-sql – 它已经为我工作。 确保你安装了所有必需的软件包 。

https://github.com/tjanczuk/edge

https://github.com/tjanczuk/edge-sql

从那里,它很蒸汽。

 var edge = require('edge'); var params = { connectionString: "Server=YourServer;Database=YourDB;Integrated Security=True", source: "SELECT TOP 20 * FROM SampleData" }; var getData = edge.func( 'sql', params); getData(null, function (error, result) { if (error) { console.log(error); return; } if (result) { console.log(result); } else { console.log("No results"); } }); 

编辑

那么…我原来的答案10天后,显然mssql添加Windows身份validation到包。 他们听到我们的哭声:) 看到这里 。 我还没有testing过,但正式在我的积压testing集成。 我会回报。

FWTW,如果mssql满足您的需求,我会去用它,因为1) edge-sql已经hibernate了2年,2)主要贡献者说他已经离开这样的项目“ 在微软的关心的手中 ”,因为他不再在那里工作。

编辑2

这不断得到upvotes和有评论说,其他答案的代码示例的一些不工作或不在Windows上工作。

这是我的代码使用mssql ,在Windows上工作,同时安装了msnodesqlv8

 var sql = require('mssql/msnodesqlv8'); var config = { driver: 'msnodesqlv8', connectionString: 'Driver={SQL Server Native Client XX.0};Server={SERVER\\NAME};Database={dbName};Trusted_Connection={yes};', }; sql.connect(config) .then(function() { ...profit... }) .catch(function(err) { // ... connect error checks }); 

由于这是一个相当明显的答案,我想添加一个代码片段,为我工作的可信连接。 从getglad的编辑答案得到它。

 const sql = require("mssql"); require("msnodesqlv8"); const conn = new sql.Connection({ database: "db_name", server: "server_name", driver: "msnodesqlv8", options: { trustedConnection: true } }); conn.connect().then(() => { // ... sproc call, error catching, etc // example: https://github.com/patriksimek/node-mssql#request }); 

使用受信任的连接,我能够执行存储过程,logging输出,并closures连接没有任何麻烦,并且msnodesqlv8已经比其他任何驱动程序更新(最新发布是2016年10月截止到11/3/2016 ),所以这似乎也是一个安全的select。

这里是一个使用mssql@4.0.4的例子。 唯一的变化是最初的需求,它从mssql内部提取msnodesqlv8,而sql.Connection现在是sql.ConnectionPool。 您还需要更改您的存储过程调用,因为响应不同,请注意这里 。 感谢Jon的回答,因为他在我之前更新了我的答案!

 const sql = require("mssql/msnodesqlv8"); const conn = new sql.ConnectionPool({ database: "db_name", server: "server_name", driver: "msnodesqlv8", options: { trustedConnection: true } }); conn.connect().then(() => { // ... sproc call, error catching, etc // example: https://github.com/patriksimek/node-mssql#request }); 

我一直在挣扎一段时间,关于如何使用mssql + Windowsauthentication,这是我如何得到它在我的项目上工作。

正如在mssql文档中指出的,你也需要安装msnodesqlv8。

 npm install msnodesqlv8 

现在,继Aaron Ballard的回答之后 ,你就这样使用它:

 const sql = require('mssql/msnodesqlv8') const pool = new sql.ConnectionPool({ database: 'database', server: 'server', driver: 'msnodesqlv8', options: { trustedConnection: true } }) pool.connect().then(() => { //simple query pool.request().query('select 1 as number', (err, result) => { console.dir(result) }) }) 

作为一个说明,我试图把这个作为对Aaron答案的一个评论,因为我只是对他的补充/更新,但是我没有足够的声望去这样做。

我努力的连接到使用Windows身份validation模式在远程Windows服务器上运行的mssql服务器。 然后我find了像下面的代码一样使用的解决scheme。

 sql.connect("Data Source=172.25.xx,1433;User Id=CSLx\\Name;Password=xxxxxx1234;Initial Catalog=giveTHedataabseNamel;Integrated Security=True",function(err){ }