您好,我无法连接到在 Node js 中使用 Windows 身份验证的 SQL Server。我正在使用 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();
最佳答案
由于这是一个相当明显的答案,因此我想添加一个代码片段,该代码片段适用于我的 Trusted Connection。从 getglad's 得到它编辑答案。
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
});
使用可信连接,我能够毫无问题地执行存储过程、记录输出并关闭连接,msnodesqlv8比其他任何驱动程序都更新得最近(最新版本是 2016 年 10 月,截至 2016 年 11 月 3 日),因此这似乎也是一个安全的选择。
这是一个使用 mssql@4.0.4 的示例。唯一的变化是最初的要求,它从 mssql 中拉入 msnodesqlv8,并且 sql.Connection 现在是 sql.ConnectionPool。由于响应不同,您还需要更改存储过程调用,注意 here .感谢 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
});
关于sql-server - 如何使用 mssql 模块通过 Node.JS 的 Windows 身份验证连接到 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33709807/