適用於 Node.js 的 Azure SQL 模組Azure SQL modules for Node.js

從 Node.js 使用儲存在 Azure SQL Database 中的資料。Work with data stored in Azure SQL Database from Node.js. 管理程式庫提供的介面,可讓您輕鬆地管理 Microsoft Azure SQL 資料庫。The management library provides an interface to make it easy to manage Microsoft Azure SQL databases.

用戶端封裝Client package

安裝 npm 模組Install the npm module

安裝 SQL Server 用戶端 npm 模組Install the SQL Server client npm module

npm install tedious

範例Example

此範例會連線到 SQL Server 資料庫並執行簡單查詢。This example connects to a SQL Server database and perform a simple query.

const Connection = require('tedious').Connection;
const Request = require('tedious').Request;

const config = {
  userName: 'your-username',
  password: 'your-password',
  server: 'path-to-server',
  options: {
    database: 'database-name',
    encrypt: true
  }
};

const connection = new Connection(config);
connection.on('connect', err => {
  err ? console.log(err) : executeStatement();
});

const query = 'SELECT * from TableName';
const executeStatement = () => {
  const request = new Request(query, (err, rowCount) => {
    err ? console.log(err) : console.log(rowCount);
  });

  request.on('row', columns => {
    columns.forEach(column => console.log(column.value));
  });

  connection.execSql(request);
};

管理封裝Management package

安裝 npm 模組Install npm modules

安裝 Azure SQL Server 管理 npm 模組Install the Azure SQL Server management npm module

npm install azure-arm-sql

範例Example

驗證、建立用戶端,並列出所有伺服器。Authenticate, create a client, and list all servers.

const msRestAzure = require('ms-rest-azure');
const SQLManagement = require('azure-arm-sql');

msRestAzure
  .interactiveLogin()
  .then(credentials => {
    const client = new SQLManagement(credentials, 'your-subscription-id');
    return client.servers.list();
  })
  .then(servers => console.dir(servers, { depth: null, colors: true }))
  .catch(err => console.log(err));

範例Samples

深入探索可在應用程式中使用的 Node.js 程式碼範例Explore more sample Node.js code you can use in your apps.