node.js SQL Server 连接到节点 js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41477386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:54:16  来源:igfitidea点击:

SQL Server connection to node js

sql-servernode.jsdatabasesql-server-2005

提问by crod

I am trying to establish a connection between nodejs project and server running Microsoft SQL Server 2005. I am using a node module mssql, but I get these errors when I am attempting to create a connection:

我正在尝试在 nodejs 项目和运行 Microsoft SQL Server 2005 的服务器之间建立连接。我正在使用 node 模块mssql,但是当我尝试创建连接时出现以下错误:

{ [ConnectionError: Failed to connect to 123.123.12.1:1433 in 15000ms]
name: 'ConnectionError',
message: 'Failed to connect to 123.123.12.1:1433 in 15000ms',
code: 'ETIMEOUT' }

{ [ConnectionError: Failed to connect to 123.123.12.1:1433 in 15000ms]
name: 'ConnectionError',
message: 'Failed to connect to 123.123.12.1:1433 in 15000ms',
code: 'ETIMEOUT' }

My connection being made by

我的连接是由

var sql = require('mssql');

var dbConfig = {
    server:'123.123.12.1',
    database:'testingDB',
    user:'userName',
    password:'pass',
    port:1433
};

function getEmp() {
    var conn = new sql.Connection(dbConfig);
    var req = new sql.Request(conn);

    conn.connect(function(err) {
        if(err) {
            console.log(err);
            return;
        }
    else {
        console.log('success');
    }
});
}

getEmp();

I am not sure what I am doing wrong, I am using a cloud 9 IDE if that helps.

我不确定我做错了什么,如果有帮助,我正在使用 cloud 9 IDE。

回答by Saroj

Put your var req = new sql.Request(conn)inside connect.

把你的var req = new sql.Request(conn)内部连接。

// config for your database
var config = {
    user: 'sa',
    password: 'mypassword',
    server: 'localhost', 
    database: 'SchoolDB' 
};

// connect to your database
sql.connect(config, function (err) {

    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query('select * from Student', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

    });
});

回答by reza.cse08

It work for me. First install mssql by npm (npm install --save mssql)

它对我有用。首先通过 npm 安装 mssql ( npm install --save mssql)

var sql = require('mssql');

for local sql server

对于本地 sql 服务器

var sqlConfig = {
  user: 'sa',
  password: 'admin',
  server: 'CBMOBILESHAMIM\SQLEXPRESS',  
  database: 'databaseName'
};

for azure server

天蓝色服务器

var sqlConfig = {
  user: 'adminLogin',
  password: 'admin',
  server: 'severname.database.windows.net',    // don't add tcp & port number
  database: 'databaseName',
  options: {
    encrypt: true
  }
};

Now connect to server

现在连接到服务器

(async function () {
  try {
    console.log("sql connecting......")
    let pool = await sql.connect(sqlConfig)
    let result = await pool.request()
      .query('select * from Subject')  // subject is my database table name

    console.log(result )

  } catch (err) {
    console.log(err);
  }
})()

for more details check mssql

有关更多详细信息,请查看mssql

回答by AkashDeep

var webconfig = {

user: 'login',

password: 'sa@123',

server: 'localhost', 

database: 'TestDB',



options: {

    encrypt: false // Use this if you're on Windows Azure 

}

  }




 var express = require('express');

 var sql = require('mssql');

 var http = require('http');


var connection = new sql.Connection(webconfig, function(err) {
var request = new sql.Request(connection); 
request.query('select * from Users', function(err, recordset) {
   if(err)      // ... error checks 
        console.log('Database connection error');

console.dir("User Data: "+recordset);
});
 });

 var app = express();

 var port = process.env.PORT || 8000;

or visit here : https://nodejsbeginersprograms.blogspot.in/2017/02/nodejs-basic-tutorial-with-mssql.html

或访问这里:https: //nodejsbeginersprograms.blogspot.in/2017/02/nodejs-basic-tutorial-with-mssql.html