无法使用繁琐的 Node.js 连接到 SQL 服务器

Cannot Connect to SQL Server using tedious Node.js

所以我的学校有一个 SQL 服务器 "SQL.SCHOOL.EDU\STUDENTSQLSERVER,4500" 而我终究无法弄清楚如何使用繁琐的方式连接到它。 我有一个快速后端,它在前端使用 React。然而,我目前只在 express index.js 上工作,试图从服务器和最终数据库中获得某种反馈。

这是我的 index.js 完整文件

var express = require('express');
var router = express.Router();
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES; 


/* DBConfig */
var config = {
  server: 'SQL.SCHOOL.EDU',
  userName: 'user1',
  password: 'password1',
  debug: true,
  connectionTimeout: 300000,
  requestTimeout: 300000,
  options: {
    database: 'user1',
    instanceName: 'STUDENTSQLSERVER', 
  },
  port: 4500,
};
/*Get users from DB */
function getUsers() {
  var connection = new Connection(config);

  connection.on('connect', function(err) {
    if(err) {
      console.log('Error: ', err)
    }
    // If no error, then good to go...
    request = new Request("select * from 'Users'", function(err) {
      if (err) {
        console.log(err);
      }});
    var result ="";
    request.on('row', function(columns) {
      columns.forEach(function(column) {
        if (column.value === null) {  
          console.log('NULL');  
        } else {  
          result+= column.value + " ";  
        }
      });
      console.log(result);
    });

    request.on('done', function(rowCount, more) {  
      console.log(rowCount + ' rows returned');  
      });

    connection.execSql(request);
  });


}

/* GET home page. */
router.get('/Users', (req, res) => {
  getUsers();
  const users = [

    {id:1, userName: "Lee", age: 23, email: "Lee@email.com"},
    {id:2, userName:  "Bob", age: 22, email: "Bobo@email.com"},
    {id:3, userName:  "James", age: 32, email: "james@email.com"},
    {id:2, userName:  "Lauren", age: 26, email: "lren@email.com"},
    {id:2, userName:  "t-dawg", age: 20, email: "dogg@email.com"},
  ]
  res.json(users)
});




module.exports = router;

连接继续超时,但我和我的同学已经能够使用 .net 连接,并且 php。

如果有人能帮助我,那就太好了,我尝试将整个服务器名称包含在 "server:" 中,并将端口放在 "port:" 中,但无济于事

感谢您的帮助。

有一个名为 mssql (https://www.npmjs.com/package/mssql) 的 npm 包,我建议像这样使用它。像这样为您的配置创建一个单独的 .json 文件:

config.json

{
 "mssql": {
   "server": 'SQL.SCHOOL.EDU',
   "userName": 'user1',
   "password": 'password1',
   "database": 'user1',
   "debug": true,
   "connectionTimeout": 300000,
   "requestTimeout": 300000,
   "options": {
    "instanceName": 'STUDENTSQLSERVER', 
   },
  "port": 4500
 }

然后你的 index.js 可以像这样使用它:

const path = require('path');
const express = require('express');
const router = require('express').Router();
const sql = require('mssql');
const sql_config = require(path.join("..", "config", "config.json")).mssql;
const pool = new sql.ConnectionPool(sql_config);
pool.connect(err => { if (err) throw err });

function getUsers() {
 let get_request = new sql.Request(pool);
 get_request.query(`select * from table1`, (err,result) => {
  if(err) {
   *handle error*
  }
  *do stuff with result here*
  })
}