使用 mssql for node.js 连接到 SQL Server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21204411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to SQL Server with mssql for node.js
提问by user3209808
I got a error message when I connect to SQL Server with mssqlmodule for node.js.
当我使用mssqlnode.js 模块连接到 SQL Server 时收到一条错误消息。
[Error: connection to 192.168.1.101\sql:1433 - failed Error: getaddrinfo ENOENT]
[错误:连接到 192.168.1.101\sql:1433 - 失败错误:getaddrinfo ENOENT]
var config = {
//driver: 'msnodesql',
user: '...',
password: '...',
server: '192.168.1.101\sql',
//TCP/IP 127.0.0.1
database: 'ACCOUNTDB'
};
回答by Patrik ?imek
You should be able to connect to named instance when using tedious driver with this config:
使用带有此配置的乏味驱动程序时,您应该能够连接到命名实例:
var config = {
user: '...',
password: '...',
server: '192.168.1.101',
driver: 'tedious',
database: 'ACCOUNTDB',
options: {
instanceName: 'sql'
}
};
Documentationalso says:
文档还说:
The SQL Server Browser service must be running on the database server, and UDP port 1444 on the database server must be reachable.
SQL Server Browser 服务必须在数据库服务器上运行,并且必须可以访问数据库服务器上的 UDP 端口 1444。
Config for msnodesql driver is little more complicated because it's connection string doesn't support named instances by default (should change in future):
msnodesql 驱动程序的配置稍微复杂一点,因为它的连接字符串默认不支持命名实例(将来应该会更改):
var config = {
user: '...',
password: '...',
server: '192.168.1.101',
driver: 'msnodesql',
database: 'ACCOUNTDB',
connectionString: "Driver={SQL Server Native Client 11.0};Server=#{server}\sql;Database=#{database};Uid=#{user};Pwd=#{password};"
};
回答by user1781272
Something that caught me is the fact that port and instanceName are mutually exclusive. Meaning that you only need one or the other.
吸引我的是 port 和 instanceName 是互斥的。这意味着您只需要其中一个。
回答by Cassius Horvath
Ok, I had the same issue, will try to help. this is my config exemple
好的,我遇到了同样的问题,会尽力提供帮助。这是我的配置示例
const config = {
user: 'sa',
password: '****',
server: 'DESKTOP-Q5TO47P',
database: 'dbname',
options: {
encrypt: false
}
};
You need to turn on the SQL Server Browser. Go to start up menu or the search and look for SQL Server Configuration Manager. Run it! (Im using 2018 version)
您需要打开 SQL Server 浏览器。转到启动菜单或搜索并查找 SQL Server 配置管理器。运行!(我用的是2018版)
- In the left Tab click on SQL Server Services
- now in the right tab double click on SQL Server Browser
- will open a window, you will see 3 tabs, go for the Service tab
- change start mode to Automatic and apply
- left click on SQL Server Browser and click restart
- Back to the right tab click on SQL Server Network Configuration
- then Client Protocols
- change TCP/IP to enable
- 在左侧选项卡中单击 SQL Server 服务
- 现在在右侧选项卡中双击 SQL Server Browser
- 将打开一个窗口,您将看到 3 个选项卡,转到服务选项卡
- 将启动模式更改为自动并应用
- 左键单击 SQL Server Browser 并单击重新启动
- 返回右侧选项卡,单击 SQL Server 网络配置
- 然后是客户端协议
- 更改 TCP/IP 以启用

