我正在运行以下代码,好像连接已成功建立,但是我看不到任何输出,或者没有任何方法可以查看查询结果的输出,我是这个量角器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 }
答案 0 :(得分:0)
我在他们的官方文档中没有找到有关方法request.send(recordset);
的任何信息。
请求对象中支持的方法是:
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);
}
});
});