×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Paul Allies Allies
Added: Dec 17, 2013 10:37 PM
Modified: Dec 17, 2013 10:42 PM
Views: 35
Communication between nodejs and Microsoft Sql Server on windows 8 machine using edge and edge-sql
  1. var edge = require("edge");
  2. process.env.EDGE_SQL_CONNECTION_STRING="Data Source=localhost\\sqlexpress;Initial Catalog=db1;user id=***;password=***";
  3.  
  4. var getCount = function(cb){
  5.         edge.func('sql', function () {/*
  6.             select count(*) from tbl_suppliers
  7.         */})(null, function (error, result) {
  8.             cb(error,result);
  9.         });
  10. };
  11.  
  12. var getSuppliers = function(cb){
  13.         edge.func('sql', function () {/*
  14.             select * from tbl_suppliers
  15.         */})(null, function (error, result) {
  16.             cb(error,result);
  17.         });
  18. };
  19.  
  20. var getPagedSuppliers = function(pageObject,cb){
  21.         edge.func('sql', function () {/*
  22.                 SELECT *
  23.                         FROM (
  24.                 SELECT *, ROW_NUMBER() OVER (ORDER BY id) AS RowNum
  25.                         FROM tbl_suppliers ) AS SOD
  26.                         WHERE SOD.RowNum BETWEEN ((@PageNumber-1)*@RowsPerPage)+1
  27.                 AND @RowsPerPage*(@PageNumber)
  28.         */})(pageObject, function (error, result) {
  29.  
  30.             cb(error,sqlformat(result));
  31.         });
  32. };
  33.  
  34. var sqlformat = function(result){
  35.         var collection = [];
  36.         for(var i = 1; i < result.length; i++){
  37.                 var item = {};
  38.                 for(var j = 0; j < result[0].length; j++){
  39.                         item[result[0][j]] = result[i][j];
  40.                 }      
  41.                 collection.push(item); 
  42.         }
  43.         return collection;
  44. }
  45.  
  46.  
  47.  
  48. getPagedSuppliers({PageNumber:1, RowsPerPage:5},function(err, result){
  49.         if (err) throw err;
  50.         console.log(result);
  51. });