利用 Node.js 与 SQL Server 构建应用程序全解析
1. 环境搭建
要使用 Node.js 连接 SQL Server,首先需要安装 Tedious 驱动并准备好环境。按照微软文档(www.microsoft.com/sql-server/developer-get-started/node/windows/step/2.html)的说明,可以在 Visual Studio Code 的集成终端中,在保存程序的文件夹里执行以下命令:
npm init -y npm install tedious npm install async关于使用 Tedious 驱动为 Node.js 与 SQL Server 编写程序的文档参考,可查看 API 文档(http://tediousjs.github.io/tedious/index.html),更多示例可在(https://github.com/tediousjs/tedious/tree/master/examples)找到。
2. 连接到 SQL Server
使用 Node.js 连接 SQL Server 依赖于 Tedious 驱动的 Connection 对象。以下是连接到 Linux 上的 SQL Server 并使用默认数据库WideWorldImporters的代码示例,可在 Visual Studio Code 编辑器中打开customerappconnect.js文件查看: