Nodejs is a simple encapsulation operation example for connecting to the sqlserver database based on the mssql module, nodejssqlserver

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.