javascript connect to azure database

Solutions on MaxInterview for javascript connect to azure database by the best coders in the world

showing results for - "javascript connect to azure database"
Jana
29 Mar 2019
1const { Connection, Request } = require("tedious");
2
3// Create connection to database
4const config = {
5  authentication: {
6    options: {
7      userName: "username", // update me
8      password: "password" // update me
9    },
10    type: "default"
11  },
12  server: "your_server.database.windows.net", // update me
13  options: {
14    database: "your_database", //update me
15    encrypt: true
16  }
17};
18
19const connection = new Connection(config);
20
21// Attempt to connect and execute queries if connection goes through
22connection.on("connect", err => {
23  if (err) {
24    console.error(err.message);
25  } else {
26    queryDatabase();
27  }
28});
29
30function queryDatabase() {
31  console.log("Reading rows from the Table...");
32
33  // Read all rows from table
34  const request = new Request(
35    `SELECT TOP 20 pc.Name as CategoryName,
36                   p.name as ProductName
37     FROM [SalesLT].[ProductCategory] pc
38     JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid`,
39    (err, rowCount) => {
40      if (err) {
41        console.error(err.message);
42      } else {
43        console.log(`${rowCount} row(s) returned`);
44      }
45    }
46  );
47
48  request.on("row", columns => {
49    columns.forEach(column => {
50      console.log("%s\t%s", column.metadata.colName, column.value);
51    });
52  });
53
54  connection.execSql(request);
55}
56