从MS Sql请求表-Node.js Web服务

时间:2019-06-25 17:13:18

标签: javascript node.js sql-server express sql-server-2008

在通过Web服务(内置于Node.js中)向Microsoft SQL Server生成请求方面,我需要帮助。

总结一下眼前的问题,如下所示:我目前正在使用Windows身份验证帐户登录MS Sql Server管理-这都是find和dandy,但是,我现在尝试构建一个允许用于某些表的选择和事务处理,这是我现在遇到的问题,特别是在登录和将数据提取到Web服务方面。

代码

var express = require('express'); var app = express();
app.get('/',function(req,res) {
  const sql = require('mssql');
  // Connection Path
  const myServerPath = String("xxxx\\WS1SQLEXPRESS");
  // Connection String Parameter
  const config = {
      // User Login Details - Windows Auth or General User Account
      user    : 'xxxx-xxx\\AdrianH',
      password: 'xxxxxx',
      // Server path to connect to
      server  : myServerPath,
      // Database
      datebase: 'plex',
      options : {
        trustedConnection: true
      }
  };
  sql.connect(config,function(err) {
    if (err) console.log(err);
    // Create Request Object
    var request = new sql.Request();
    // Query the Database
    request.query('USE plex; SELECT * FROM [plex].[dbo].[tblWorkCenters]',function(err,recordset) {
      if (err) console.log(err)
      // send records as response
      res.send(recordset);
    });
  });
});
// Start Server and listen on //http://localhost:8001/
var server  = app.listen(3213,function(){
  console.log('Server is running...');
});

我隐藏了敏感信息,这是错误代码

{ ConnectionError: Login failed for user ''.
    at Connection.tedious.once.err (C:\Users\adrianh\node_modules\mssql\lib\tedious.js:244:17)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at Connection.processLogin7Response (C:\Users\adrianh\node_modules\tedious\lib\connection.js:1397:14)
    at Connection.message (C:\Users\adrianh\node_modules\tedious\lib\connection.js:1932:14)
    at Connection.dispatchEvent (C:\Users\adrianh\node_modules\tedious\lib\connection.js:1084:36)
    at MessageIO.messageIo.on (C:\Users\adrianh\node_modules\tedious\lib\connection.js:984:14)
    at MessageIO.emit (events.js:198:13)
    at Message.message.on (C:\Users\adrianh\node_modules\tedious\lib\message-io.js:32:14)
    at Message.emit (events.js:203:15)
  code: 'ELOGIN',
  originalError:
   { ConnectionError: Login failed for user ''.
       at ConnectionError (C:\Users\adrianh\node_modules\tedious\lib\errors.js:13:12)
       at Parser.tokenStreamParser.on.token (C:\Users\adrianh\node_modules\tedious\lib\connection.js:735:29)
       at Parser.emit (events.js:198:13)
       at Parser.parser.on.token (C:\Users\adrianh\node_modules\tedious\lib\token\token-stream-parser.js:27:14)
       at Parser.emit (events.js:198:13)
       at addChunk (C:\Users\adrianh\node_modules\readable-stream\lib\_stream_readable.js:297:12)
       at readableAddChunk (C:\Users\adrianh\node_modules\readable-stream\lib\_stream_readable.js:279:11)
       at Parser.Readable.push (C:\Users\adrianh\node_modules\readable-stream\lib\_stream_readable.js:240:10)
       at Parser.Transform.push (C:\Users\adrianh\node_modules\readable-stream\lib\_stream_transform.js:139:32)
       at doneParsing (C:\Users\adrianh\node_modules\tedious\lib\token\stream-parser.js:80:14) message: 'Login failed for user \'\'.', code: 'ELOGIN' },
  name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
    at Request._query (C:\Users\adrianh\node_modules\mssql\lib\base.js:1399:37)
    at Request._query (C:\Users\adrianh\node_modules\mssql\lib\tedious.js:546:11)
    at Request.query (C:\Users\adrianh\node_modules\mssql\lib\base.js:1335:12)
    at C:\Users\adrianh\Desktop\JEC_Current_Projects\WebService\WCWebServiceIOS.js:30:13
    at _poolCreate.then.catch.err (C:\Users\adrianh\node_modules\mssql\lib\base.js:287:7)
    at process._tickCallback (internal/process/next_tick.js:68:7) code: 'ECONNCLOSED', name: 'ConnectionError' }

**有趣的注释在-

(C:\Users\adrianh\node_modules\tedious\lib\token\stream-parser.js:80:14) message: 'Login failed for user \'\'.', code: 'ELOGIN' },
      name: 'ConnectionError' }

它似乎并没有传递我的登录信息-非常感谢您的帮助。

Microsoft SQL Login screen

1 个答案:

答案 0 :(得分:1)

如果您试图通过节点JS中的Windows身份验证连接mssql,请使用此模块。

var mssql = require('mssql/msnodesqlv8

示例:

var mssql = require('mssql/msnodesqlv8')

var dbConfig = {    
    server: 'server',
    driver: 'msnodesqlv8',
    database: 'DBDATA', 
    port: '1433',
    options: {
        trustedConnection: true,              
        debug: {
            packet: false,
            payload: false,
            token: false,
            data: false
        },     
    }
};