如何使用MSSQL模块,Windows身份验证连接到SQL Server从Node.js的
嗨,我是无法连接到在节点JS使用Windows身份验证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();
回答如下:
由于这是一个相当明显的答案,我想在与信任的连接为我工作的代码片段添加。得到它从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/patriksimek/node-mssql#request
});
使用受信任的连接,我可以执行存储过程,日志的输出,并关闭没有任何麻烦的连接,msnodesqlv8已经比任何其他车手的最近更新(最新版本是十月2016年的2016年11月3日),因此,这似乎是一个安全的选择为好。
下面是使用[email protected]一个例子。唯一的变化是最初的要求,这在msnodesqlv8拉离MSSQL中,并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/patriksimek/node-mssql#request
});