Supported container runtimes

MSSQLServer

qianmoQqianmoQ· 更新于 2026-10-19· 阅读 5 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

MSSQL Server

Install

1
npm install @testcontainers/mssqlserver --save-dev

Examples

These examples use the following libraries:

  • mssql

    1
    2
    npm install mssql
    npm install @types/mssql --save-dev

Choose an image from the container registry and substitute IMAGE.

EULA Acceptance

Due to licencing restrictions you are required to accept an EULA for this container image. To indicate that you accept the MS SQL Server image EULA, call the acceptLicense() method.

Please see the microsoft-mssql-server image documentation for a link to the EULA document.

Execute a query

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
await using container = await new MSSQLServerContainer(image).acceptLicense().start();

const sqlConfig: config = {
  user: container.getUsername(),
  password: container.getPassword(),
  database: container.getDatabase(),
  server: container.getHost(),
  port: container.getPort(),
  pool: { max: 1, min: 0, idleTimeoutMillis: 30000 },
  options: { trustServerCertificate: true },
};
const connection = await new sql.ConnectionPool(sqlConfig).connect();

const { recordset } = await connection.query`SELECT 1;`;
expect(recordset).toStrictEqual([{ "": 1 }]);

await connection.close();

Connect via URI

1
2
3
4
5
6
7
8
9
await using container = await new MSSQLServerContainer(image).acceptLicense().start();

const connectionString = container.getConnectionUri();
const connection = await new sql.ConnectionPool(connectionString).connect();

const { recordset } = await connection.query`SELECT 1;`;
expect(recordset).toStrictEqual([{ "": 1 }]);

await connection.close();

With password

1
await using container = await new MSSQLServerContainer(image).acceptLicense().withPassword("I!@M#$eCur3").start();

With different edition

1
2
3
4
5
await using container = await new MSSQLServerContainer(image)
  .acceptLicense()
  .withEnvironment({ MSSQL_PID: "Express" })
  .withWaitForMessage(/.*SQL Server is now ready for client connections.*/)
  .start();

评论

登录后参与评论

正在加载评论…