我目前正在从事一个项目,该项目需要我操作与时间报告应用程序相关的 SQL 表。我现在连接的简单代码如下:
var Connection = require('tedious').Connection;
var config = {
userName: 'my_user_name',
password: 'my_password',
server: 'server_to_access',
database: 'database_in_SQL_Server_Management_Studio'
};
var connection = new Connection(config);
connection.on('connect', function(err) {
console.log("Connected");
});
它表明建立了连接,但是当我运行以下命令时:
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var config = {
userName: 'my_user_name',
password: 'my_password',
server: 'server_to_access',
database: 'database_in_SQL_Server_Management_Studio'
};
var connection = new Connection(config);
connection.on('connect', function(err) {
console.log("Connected");
executeStatement();
});
function executeStatement() {
request = new Request("SELECT * FROM Employees", function(err, rowCount) {
if (err) {
console.log(err);
} else {
console.log(rowCount + ' rows');
}
});
request.on('row', function(columns) {
columns.forEach(function(column) {
console.log(column.value);
});
});
connection.execSql(request);
}
这是我在 cmd 中的输出:
C:\Users\name\attempt>node test.js
Connected
{ [RequestError: Requests can only be made in the LoggedIn state, not the Connec
ting state]
message: 'Requests can only be made in the LoggedIn state, not the Connecting
state',
code: 'EINVALIDSTATE' }
C:\Users\name\attempt>
管理 SQL 表的应用程序是 MS SQL Server Management Studio 2008 R2。
任何关于我做错了什么的方向都将非常感激。谢谢!