![](/img/trans.png)
[英]How to connect to SQL Server with Windows authentication from Node.JS using knex module
[英]How to connect to SQL Server with windows authentication from Node.JS using mssql module
嗨,我无法连接到在节点 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编辑的答案中得到了它。
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。 您还需要更改您的存储过程调用,因为响应不同,请在此处注明。 归功于 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
+ windows auth 为我的任何项目工作。 尝试edge
和edge-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 Auth 添加到包中。 他们听到了我们的呼喊:) 看这里。 我还没有测试它,但它正式在我的待办事项中以测试集成。 我会回来报告的。
FWTW,如果mssql
符合您的需求,我会选择它,因为 1) edge-sql
已经休眠了 2 年,2)主要贡献者说他已经将这样的项目“交给了微软的关心”,因为他不再在那里工作。
编辑 2
这不断获得赞成票,并且有评论说其他一些答案的代码示例要么不起作用,要么在 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
});
我也一直在为如何使用 mssql + Windows Auth 苦苦挣扎,这是我如何让它在我的项目中工作。
正如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 答案的评论,因为我的只是对他的补充/更新,但我没有足够的声誉来这样做。
我尝试了很多变体,这是我的完整解决方案。
我正在使用SQL 服务器 Express 。
首先,我只连接到MASTER数据库。
您只需要更改“ YOURINSTANCE\\\\SQLEXPRESS ”。
(一定要保持上面的双斜线!!!)
我也在使用集成安全。
查询完全不依赖(在您的数据库中)。
您需要添加节点包
==> NPM 安装 MSSQL和
==> NPM 安装 msnodesqlv8
希望您的连接问题将成为过去。
也许。
请。
// More here -> https://www.npmjs.com/package/mssql
var sql = require('mssql/msnodesqlv8');
var config = {
connectionString: 'Driver=SQL Server;Server=YOURINSTANCE\\SQLEXPRESS;Database=master;Trusted_Connection=true;'
};
sql.connect(config, err => {
new sql.Request().query('SELECT 1 AS justAnumber', (err, result) => {
console.log(".:The Good Place:.");
if(err) { // SQL error, but connection OK.
console.log(" Shirtballs: "+ err);
} else { // All is rosey in your garden.
console.dir(result);
};
});
});
sql.on('error', err => { // Connection borked.
console.log(".:The Bad Place:.");
console.log(" Fork: "+ err);
});
为了我
我使用了如下连接设置
"server":"",
"domain":"", //sepcify domain of your user
"port": ,
"user":"", // enter username without domain
"password":"",
"database":""
和 TS 代码
import * as sql from 'mssql';
const pool = await new sql.ConnectionPool(connection).connect();
const result = await pool.request()
.query(`SELECT count(idpart) part_computed FROM demo.PARTs;`);
pool.close();
return Promise.resolve(result.recordset);
我只能使用带有连接字符串(而不是配置对象)的 msnodesqlv8(仅限于 Windows 环境)来获得可信连接。
const sql = require("msnodesqlv8");
const connectionString = function(databaseName) {
return "Server=.;Database=" + databaseName + ";Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}";
}
sql.query(connectionString("DatabaseName"), "SELECT * FROM dbo.Table1" , (err, recordset) => {
if(err) {
// Do something with the err object.
return;
}
// else
// Do something with the recordset object.
return;
});
下面的代码对我有用......
const sql = require('mssql/msnodesqlv8')
// config for your database
var config = {
driver: 'msnodesqlv8',
server: 'serverNAme\\SQLEXPRESS',
database: 'Learn' ,
options: {
trustedConnection: true
}
};
它对我有用,需要安装 msnodesqlv8 和 mssql。 还 .......:)
var dbConfig = {
driver: 'msnodesqlv8',
server: "DESKTOP-66LO4I3",
database: "FutureHealthCareWeb",
user: "sa",
password: "pass@123",
options: {
trustedConnection: true
},
debug: true,
parseJSON: true
};
var sql = require('mssql/msnodesqlv8');
sql.connect(dbConfig, function (err) {
if (err) { console.log(JSON.stringify(err)+'..............') }
else {
console.log('Connected')
}
}
);
我努力连接使用Windows 身份验证模式在远程 Windows 服务器中运行的 mssql 服务器。 然后我找到了像下面代码一样使用的解决方案。
sql.connect("Data Source=172.25.x.x,1433;User Id=CSLx\\Name;Password=xxxxxx1234;Initial Catalog=giveTHedataabseNamel;Integrated Security=True",function(err){ }
我刚刚在配置中添加了domain: "DNAME" ,因此该配置帮助我使用 Windows 身份验证连接到 MS SQL。
const config = {
driver: 'msnodesqlv8',
domain: "DNAME",
user: 'username',
password: 'pass',
server: '7.6.225.22',
database: 'DBNAME',
requestTimeout: 3600000, //an hour
options: {
trustedConnection: true
},
debug: true,
parseJSON: true
};
此版本不需要用户名或密码。
为了使用 Windows 身份验证,我安装了 mssql 和 msnodesqlv8。
然后在我的 app.js 文件中:
const mssql = require('mssql/msnodesqlv8');
请注意,如果您使用此示例,则它是 mssql 而不是 sql。
var config = {
database:'YOUR DATABASE NAME',
server: 'localhost\\SQLEXPRESS',
driver: 'msnodesqlv8',
options: {
trustedConnection: true,
enableArithAbort: true
}
};
您需要在配置中更改数据库名称。 除此之外它应该工作。 我的例子:
app.get('/', function (req, res) {
mssql.connect(config, function (err) {
if (err) console.log(err);
var request = new mssql.Request();
request.query('select * from dbo.visit', function (err, result) {
if(err) console.log(err);
console.log(result);
});
});
});
这对我有用
const sql = require("mssql/msnodesqlv8");
const conn = new sql.ConnectionPool({
database: "DB name",
server: "server name",
driver: "msnodesqlv8",
options: {
trustedConnection: true
}
});
conn.connect().then((err) => {
if(err) throw err;
else console.log("connected");
const req = new sql.Request(conn)
req.query("select * from table", function(error, res){
console.log(res)
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.