@@ -61,8 +61,7 @@ Open a command prompt and create a folder named *sqltest*. Open the folder you c
61
61
62
62
``` bash
63
63
npm init -y
64
-
65
-
64
+ npm install tedious
66
65
```
67
66
68
67
## Add code to query database
@@ -72,63 +71,61 @@ Open a command prompt and create a folder named *sqltest*. Open the folder you c
72
71
1 . Replace its contents with the following code. Then add the appropriate values for your server, database, user, and password.
73
72
74
73
``` js
75
- var Connection = require (' tedious' ).Connection ;
76
- var Request = require (' tedious' ).Request ;
74
+ const Connection = require (" tedious" ).Connection ;
75
+ const Request = require (" tedious" ).Request ;
77
76
78
77
// Create connection to database
79
- var config =
80
- {
81
- authentication: {
82
- options: {
83
- userName: ' userName' , // update me
84
- password: ' password' // update me
85
- },
86
- type: ' default'
78
+ const config = {
79
+ authentication: {
80
+ options: {
81
+ userName: " username" , // update me
82
+ password: " password" // update me
87
83
},
88
- server: ' your_server.database.windows.net' , // update me
89
- options:
90
- {
91
- database: ' your_database' , // update me
92
- encrypt: true
93
- }
94
- }
95
- var connection = new Connection (config);
84
+ type: " default"
85
+ },
86
+ server: " your_server.database.windows.net" , // update me
87
+ options: {
88
+ database: " your_database" , // update me
89
+ encrypt: true
90
+ }
91
+ };
92
+
93
+ const connection = new Connection (config);
96
94
97
95
// Attempt to connect and execute queries if connection goes through
98
- connection .on (' connect' , function (err )
99
- {
100
- if (err)
101
- {
102
- console .log (err)
103
- }
104
- else
105
- {
106
- queryDatabase ()
107
- }
96
+ connection .on (" connect" , err => {
97
+ if (err) {
98
+ console .error (err .message );
99
+ } else {
100
+ queryDatabase ();
101
+ }
102
+ });
103
+
104
+ function queryDatabase () {
105
+ console .log (" Reading rows from the Table..." );
106
+
107
+ // Read all rows from table
108
+ const request = new Request (
109
+ ` SELECT TOP 20 pc.Name as CategoryName,
110
+ p.name as ProductName
111
+ FROM [SalesLT].[ProductCategory] pc
112
+ JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid` ,
113
+ (err , rowCount ) => {
114
+ if (err) {
115
+ console .error (err .message );
116
+ } else {
117
+ console .log (` ${ rowCount} row(s) returned` );
118
+ }
108
119
}
109
- );
110
-
111
- function queryDatabase ()
112
- {
113
- console .log (' Reading rows from the Table...' );
114
-
115
- // Read all rows from table
116
- var request = new Request (
117
- " SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc "
118
- + " JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid" ,
119
- function (err , rowCount , rows )
120
- {
121
- console .log (rowCount + ' row(s) returned' );
122
- process .exit ();
123
- }
124
- );
125
-
126
- request .on (' row' , function (columns ) {
127
- columns .forEach (function (column ) {
128
- console .log (" %s\t %s" , column .metadata .colName , column .value );
129
- });
120
+ );
121
+
122
+ request .on (" row" , columns => {
123
+ columns .forEach (column => {
124
+ console .log (" %s\t %s" , column .metadata .colName , column .value );
130
125
});
131
- connection .execSql (request);
126
+ });
127
+
128
+ connection .execSql (request);
132
129
}
133
130
```
134
131
0 commit comments