Evenementer
Mar 31, 11 PM - Apr 2, 11 PM
Dat gréisst Léier-Evenement fir SQL, Fabric a Power BI. 31. Mäerz - 2. Abrëll. Benotzt de Code FABINSIDER, fir $400 ze spueren.
Mellt Iech haut unDëse Browser gëtt net méi ënnerstëtzt.
Upgrat op Microsoft Edge fir vun de Virdeeler vun leschten Eegeschaften, Sécherheetsupdaten, an techneschem Support ze profitéieren.
This example should be considered a proof of concept only. The sample code is simplified for clarity, and does not necessarily represent best practices recommended by Microsoft. Other examples, which use the same crucial functions are available on GitHub:
The new Connection function is used to connect to SQL Database.
var Connection = require('tedious').Connection;
var config = {
server: 'your_server.database.windows.net', //update me
authentication: {
type: 'default',
options: {
userName: 'your_username', //update me
password: 'your_password' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
database: 'your_database' //update me
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
connection.connect();
All SQL statements are executed using the new Request() function. If the statement returns rows, such as a select statement, you can retrieve them using the request.on() function. If there are no rows, the request.on() function returns empty lists.
var Connection = require('tedious').Connection;
var config = {
server: 'your_server.database.windows.net', //update me
authentication: {
type: 'default',
options: {
userName: 'your_username', //update me
password: 'your_password' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
database: 'your_database' //update me
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement();
});
connection.connect();
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
function executeStatement() {
var request = new Request("SELECT c.CustomerID, c.CompanyName,COUNT(soh.SalesOrderID) AS OrderCount FROM SalesLT.Customer AS c LEFT OUTER JOIN SalesLT.SalesOrderHeader AS soh ON c.CustomerID = soh.CustomerID GROUP BY c.CustomerID, c.CompanyName ORDER BY OrderCount DESC;", function(err) {
if (err) {
console.log(err);}
});
var result = "";
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
request.on('done', function(rowCount, more) {
console.log(rowCount + ' rows returned');
});
// Close the connection after the final event emitted by the request, after the callback passes
request.on("requestCompleted", function (rowCount, more) {
connection.close();
});
connection.execSql(request);
}
In this example you will see how to execute an INSERT statement safely, passing parameters, which protect your application from SQL injection values.
var Connection = require('tedious').Connection;
var config = {
server: 'your_server.database.windows.net', //update me
authentication: {
type: 'default',
options: {
userName: 'your_username', //update me
password: 'your_password' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
database: 'your_database' //update me
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement1();
});
connection.connect();
var Request = require('tedious').Request
var TYPES = require('tedious').TYPES;
function executeStatement1() {
var request = new Request("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES (@Name, @Number, @Cost, @Price, CURRENT_TIMESTAMP);", function(err) {
if (err) {
console.log(err);}
});
request.addParameter('Name', TYPES.NVarChar,'SQL Server Express 2014');
request.addParameter('Number', TYPES.NVarChar , 'SQLEXPRESS2014');
request.addParameter('Cost', TYPES.Int, 11);
request.addParameter('Price', TYPES.Int,11);
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log("Product id of inserted item is " + column.value);
}
});
});
// Close the connection after the final event emitted by the request, after the callback passes
request.on("requestCompleted", function (rowCount, more) {
connection.close();
});
connection.execSql(request);
}
Evenementer
Mar 31, 11 PM - Apr 2, 11 PM
Dat gréisst Léier-Evenement fir SQL, Fabric a Power BI. 31. Mäerz - 2. Abrëll. Benotzt de Code FABINSIDER, fir $400 ze spueren.
Mellt Iech haut unTraining
Dokumentatioun
Step 1: Configure development environment for Node.js - Node.js driver for SQL Server
You will need to configure your development environment with the prerequisites in order to develop an application using the Node.js Driver for SQL Server.
Node.js Driver for SQL Server - Node.js driver for SQL Server
The tedious module is an open source, JavaScript implementation of the TDS protocol, which is supported by all modern versions of SQL Server.
Step 2: Create a SQL database for Node.js - Node.js driver for SQL Server
The samples in this section only work with the AdventureWorks schema, on either Microsoft SQL Server or Azure SQL Database.
Connect to and query using Node.js and mssql npm package - Azure SQL Database
Learn how to connect to a database in Azure SQL Database and query data using Node.js and mssql npm package.