如何在量角器+ VSCode中获取mssql输出

时间:2019-04-16 19:14:49

标签: node.js sql-server typescript visual-studio-code protractor

我正在运行以下代码,好像连接已成功建立,但是我看不到任何输出,或者没有任何方法可以查看查询结果的输出,我是这个量角器nodeJS MSSQL连接的新手。 / p>

const assert = require("../configuration.js");
const { config } = require("dotenv");
const { ConnectionPool } = require("mssql");
var sql = require('mssql');

config();

const c = {
  driver: 'msnodesqlv8',
  server: "server/nameORIP",
  user: "UserName",
  password: "Password",
  database: 'DBname',
};

describe("mssql connection", () => {

  it('test query', () => {

    sql.connect(config, (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 NetUsers', (err, recordset) => {

        if (err) console.log(err)
        // send records as a response
        request.send(recordset);
      });

    }); // end of sql.connect()

  }) // end of it
})

输出

Request {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  canceled: false,
  _paused: false,
  parent: [Function: ConnectionPool],
  parameters: {},
  rowsAffected: 0 }

2 个答案:

答案 0 :(得分:0)

我在他们的官方文档中没有找到有关方法request.send(recordset);的任何信息。

请求对象中支持的方法是:

  1. 执行-https://www.npmjs.com/package/mssql#execute
  2. 输入-https://www.npmjs.com/package/mssql#input
  3. 输出-https://www.npmjs.com/package/mssql#output
  4. 管道-https://www.npmjs.com/package/mssql#pipe
  5. 查询-https://www.npmjs.com/package/mssql#query
  6. 批次-https://www.npmjs.com/package/mssql#batch
  7. 批量-https://www.npmjs.com/package/mssql#bulk
  8. 取消-https://www.npmjs.com/package/mssql#cancel

request.query函数将为您提供来自数据库的记录列表,并将其传递给回调函数,并且可以使用

直接访问这些值。
request.query('select * from NetUsers', (err, resultArray) => {
    if (err) console.log(err)
    // if no error
    console.log(resultArray.recordset[0]); //will print the first row from response.
  });

引用:https://www.npmjs.com/package/mssql#query-command-callback

答案 1 :(得分:0)

我终于使它可以进行一些更改,不仅可以连接,还可以运行查询并在控制台中向我显示输出,再次感谢您的所有帮助。

import { async } from "q";
const sql = require('mssql/msnodesqlv8');
const assert = require("../configuration.js");

const poolPromise = new sql.ConnectionPool({
    driver: 'msnodesqlv8',
    server: "server/nameORIP",
    user: "UserName",
    password: "Password",
    database: 'DBname',
})
.connect()
  .then(pool => {
    console.log('Connected to MSSQL')
    return pool
  })
  .catch(err => console.log('Database Connection Failed! Bad Config: ', err))
;

describe('any test', () => {

    it('verification', async () => {

        try
        {
            const pool = await poolPromise;

            const result = await pool.request()
            .query('SELECT TOP (10) * FROM dbo.NetUsers')

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

    });


});