Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- C:Usersnameattempt>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:Usersnameattempt>
- connection.on('connect', (err)=>{ RequestWrapper.next(err); });
- const queue = [];
- class RequestWrapper(){
- constructor(sql){ this.sql = sql; }
- static next(err){
- const next = queue.shift();
- if(next) next(err);
- }
- queue(args){
- return new Promise((ok, fail)=>{
- queue.push((err)=>{
- if(err) fail(err); else ok(this.request(args));
- })
- })
- }
- request(args){
- if(!connection) return Promise.reject(new Error(`connection required`));
- if(!connection.loggedIn || connection.state.name !== 'LoggedIn') return this.queue(args);
- return new Promise((ok, fail)=>{
- try{
- const request = new tedious.Request(this.sql, (err, count, rows)=>{
- RequestWrapper.next();
- if(err) return fail(err);
- ok(rows);
- });
- ...add params here...
- connection.execSql(request);
- }catch(err){
- RequestWrapper.next();
- fail(err);
- }
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement