Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. var fs = require('fs');
  4. var Connection = require('tedious').Connection;
  5. var Request = require('tedious').Request;
  6. var config = {
  7.   server: 'DEV01',
  8.   userName: 'sa',
  9.   password: '10vs@kbsc',
  10.   'options': {
  11.     'database': 'KonektArquivos',
  12.     'instanceName': 'KBDFIDC'
  13.   }
  14. };
  15. var connection = new Connection(config);
  16. connection.on('connect', function(err) {
  17.     console.log('connected');
  18.     get_xmls();
  19.   }
  20. );
  21. connection.on('debug', function(err) { console.log('debug:', err);});
  22.  
  23. function get_xmls() {
  24.   var request = new Request(`
  25.     SELECT TOP 200 n.Chave, a.danfe
  26.     FROM NfeArquivo a
  27.     INNER JOIN Nfe n ON n.id = a.id
  28.     ORDER BY a.id DESC;`, function(err, rowCount) {
  29.     if (err) {
  30.       console.log(err);
  31.     }
  32.   });
  33.   request.on('row', function(columns) {
  34.     var chave = columns[0].value;
  35.     var xml = columns[1].value;
  36.     fs.writeFile('xml/' + chave + '.xml', xml, function (err) {
  37.       if (err) throw err;
  38.       console.log(chave + ' Saved!');
  39.     });
  40.   });
  41.   request.on('done', function(rowCount, more) {
  42.     console.log(rowCount + ' rows returned');
  43.     connection.close();
  44.   });
  45.   connection.execSql(request);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement