Nodejs is a simple encapsulation operation example for connecting to the sqlserver database based on the mssql module, nodejssqlserver
This example describes how to connect node. js to an SQL Server database based on the mssql module. We will share this with you for your reference. The details are as follows:
Note: To enable the SQL Server to allow remote connection, follow the steps described in Baidu. With many experiences, nodejs can connect to SQL server. It is best to disable firewall inbound rules or allow inbound traffic. this encapsulation is only an SQL statement query. You can understand this. Other packages can be expanded on your own and support connection pools.
1. Install the mssql Module
npm install mssql
2. database connection code
Db. js:
/* July 14, 2016 17:02:15 QQ: 452076103 yijinxi mssql module simple encapsulation */var mssql = require ('mssql'); var db ={}; var config = {user: 'sa ', password: '000000', server: '10. 81.36.167 ', database: 'admanager', port: 1433, options: {encrypt: true // Use this if you're on Windows Azure}, pool: {min: 0, max: 10, idleTimeoutMillis: 3000 }}; // run the SQL statement to return data. db. SQL = function (SQL, callBack) {var connection = new mssql. connection (config, function (err) {if (err) {console. log (err); return;} var ps = new mssql. preparedStatement (connection); ps. prepare (SQL, function (err) {if (err) {console. log (err); return;} ps.exe cute ('', function (err, result) {if (err) {console. log (err); return;} ps. unprepare (function (err) {if (err) {console. log (err); callback (err, null); return ;}callback (err, result) ;}) ;}; module. exports = db;
Test 3
Dbtest. js
Var db = require ('. /db'); db. SQL ('select * from xruserset', function (err, result) {if (err) {console. log (err); return;} console. log ('total users: ', result. length );});
Iv. Running results
In my admanager library, there are 15 users in the XRuserSet table.
I hope this article will help you design nodejs programs.