Skip to main content

使用 JavaScript 和 SQL Server 连接数据库

要连接数据库,你需要将 Prisma 架构中 datasource 块的 url 字段设置为数据库 连接网址

¥To connect your database, you need to set the url field of the datasource block in your Prisma schema to your database connection URL:

prisma/schema.prisma
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}

在本例中,url.env 中定义的 通过环境变量设置

¥In this case, the url is set via an environment variable which is defined in .env:

下面的例子连接 URL 使用 SQL 身份验证,但是有 设置连接 URL 格式的其他方法

¥The following example connection URL uses SQL authentication, but there are other ways to format your connection URL

.env
  DATABASE_URL="sqlserver://localhost:1433;database=mydb;user=sa;password=r@ndomP@$$w0rd;trustServerCertificate=true"
info

我们建议将 .env 添加到 .gitignore 文件中,以防止提交环境变量。

¥We recommend adding .env to your .gitignore file to prevent committing your environment variables.

调整连接 URL 以匹配你的设置 - 请参阅 Microsoft SQL Server 连接 URL 了解更多信息。

¥Adjust the connection URL to match your setup - see Microsoft SQL Server connection URL for more information.

确保通过 SQL Server 配置管理器 启用 TCP/IP 连接以避免 No connection could be made because the target machine actively refused it. (os error 10061)

¥Make sure TCP/IP connections are enabled via SQL Server Configuration Manager to avoid No connection could be made because the target machine actively refused it. (os error 10061)