Sometimes, we want to use Microsoft SQL Server use with Node.js.
In this article, we’ll look at how to use Microsoft SQL Server use with Node.js.
How to use Microsoft SQL Server with Node.js?
To use Microsoft SQL Server use with Node.js, we can use the node-mssql package.
To install it, we run
npm install mssql
Then we use it by writing
const sql = require("mssql");
const config = {
server: "192.168.1.212",
user: "test",
password: "test",
};
sql.connect(config, (err) => {
const request = new sql.Request();
request.query("select 'hello world'", (err, recordset) => {
console.log(recordset);
});
});
We call sql.connect to make the database connection with the connection info specified in config.
Then in the callback, we make a sql.Request instance and call query on it to make a SQL query to the Microsoft SQL Server.
We call query with a SQL query string and a callback to get the returned results from recordset.
Conclusion
To use Microsoft SQL Server use with Node.js, we can use the node-mssql package.