简体   繁体   中英

Cannot Connect to SQL Server using tedious Node.js

So my school has an SQL Server "SQL.SCHOOL.EDU\STUDENTSQLSERVER,4500" and I cannot for the life of me figure out how to connect to it using tedious. I have an express back end that is using react for the front end side of things. However I am currently only working on the express index.js to try and get some sort of feedback from the Server and eventually the data base.

Here is my index.js file in its entirity

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;

The connection continues to time out but I have and my classmates have been able to connect using .net, and php.

If anyone can help me that would be great, I have tried to include the entire server name within the "server:" and placed the port within the "port:" but to no avail

Thanks for the help.

There is an npm package called mssql ( https://www.npmjs.com/package/mssql ) I would recommend using that like this. Create a separate.json file for your config like this:

config.json

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

Then your index.js can use that like this:

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*
  })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM