我在 node.js 中使用 Microsoft 的 node-sqlserver 连接到我网络上计算机上的数据库时遇到问题。我在 windows 2008 服务器上使用 sql server 2008。我在同一域内的不同机器上远程运行我的 node.js。我认为问题与连接字符串有关。
var sql = require('node-sqlserver');
var conn_str = 'Driver={SQL Server Native Client 10.0};Server=(win08-srv);Database=DB;Trusted_Connection={Yes}';
此外,应该有一个用户名和密码才能进入数据库,但这不在 Node 模块的示例连接字符串中。
//open the DB connection
sql.open(conn_str, function (err, conn) {
if (err) {
console.log("Error opening the connection!");
return;
} else {
console.log('Connection successful!');
return;
}
});
运行时总是打印Error opening the connection!
。
最佳答案
我遇到了同样的问题。为了诊断问题,您需要做的第一件事是更改示例代码,如下所示:
sql.open(conn_str, function (err, conn) {
if (err) {
console.log("Error opening the connection! Error was: " + err);
return;
}
完成上述操作后,我获得了有关错误的更多信息。我的错误是:
"Error opening the connection! Error: IM002: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
这基本上意味着您在连接字符串中指定了错误的驱动程序。我通过将驱动程序版本从 11.0 更改为 10.0 解决了我的问题。您可以查看本地安装了哪些驱动程序(如果您使用的是 Windows,我假设您是),方法是:
控制面板 > 管理工具 > 数据源 (ODBC) > 驱动程序(选项卡)。
查找 SQL Server Native Client 并查看版本号。如果您没有安装此驱动程序,则需要下载它。
关于sql-server-2008 - 在 node.js 中使用 node-sqlserver 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11561180/