Home  >  Article  >  Web Front-end  >  Detailed example of how node.js obtains SQL Server database

Detailed example of how node.js obtains SQL Server database

零到壹度
零到壹度Original
2018-04-02 16:02:442837browse

本篇文章给大家分享的是实例详解node.js如何获取SQL Server数据库,内容挺不错的,希望可以帮助到有需要的朋友。

1.在package里引入mssql,并且用npm install 安装

2.在module文件夹下新建一个db.js的文件

<span style="font-size: 16px; color: rgb(255, 255, 255);">var mssql = require('mssql');<br>var db = {};<br>var config = {   <br>     user: '登陆名',    <br>     password: '登陆密码',    <br>     server: '服务器名称',    <br>     port:1433,    <br>     driver: 'msnodesql',    <br>     database: '数据库名称',    <br>     connectionString: "Driver={SQL Server Native Client 11.0};<br>     Server=#{server}\\sql;Database=#{database};Uid=#{user};Pwd=#{password};",   <br>  /*    options: {  <br>          encrypt: true // Use this if you're on Windows Azure   <br>   },*/   <br>    pool: {   <br>         min: 0,        <br>         max: 10,        <br>         idleTimeoutMillis: 3000   <br>   }<br>};<br>db.sql = function (sql, callBack) {   <br>   var connection = new mssql.ConnectionPool(config, function (err) {     <br>      if (err) {        <br>          console.log(err);         <br>             return;       <br>          }    <br>              var ps = new mssql.PreparedStatement(connection);       <br>              ps.prepare(sql, function (err) {         <br>                 if (err){              <br>                   console.log(err);              <br>                     return;            <br>           }<br>           <br>            ps.execute('', function (err, result) {             <br>               if (err){                    <br>                   console.log(err);                    <br>                   return;                <br>           <br>           }<br><br>            ps.unprepare(function (err) {            <br>                if (err){                       <br>                 console.log(err);                        <br>                 callback(err,null);                        <br>                 return;                    <br>          }<br>          <br>                    callBack(err, result);                <br>                    });            <br>              });        <br>        });    <br> });};<br> module.exports = db;</span><span style="font-size: 16px;"><br></span>

3.使用db   在要用的文件里引入db

var db = require(&#39;../module/db&#39;);//引入mssqlrouter.get(&#39;/&#39;,(req,res) => {
db.sql(&#39;select * from 表的名称&#39;,function(err,result){
    var data = result.recordset;//把数据的部分提取出来
    res.render(&#39;index.ejs&#39;,{data:data});
    if (err) {        console.log(err);        return;    }

    });});module.exports = router;//把router暴露出去

4.在页面展现

<span style="font-size: 16px;">2e0fb2270176fd50ed8e33c14b9224b6<br>     2cb3bcf7722a2e57c277c74689826f38<br>71f15573c02ea6f4baf6f33170bb7788<br></span>


相关推荐:

nodejs连接sqlserver数据库支持事物封装-mssql模块 

Node.js运用mssql模块链接SQL Server数据库

The above is the detailed content of Detailed example of how node.js obtains SQL Server database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn