Showing posts with label NodeJS. Show all posts
Showing posts with label NodeJS. Show all posts

Monday, 25 September 2017

expressjs API with tedious and SQL server connection

In below example I have used node.js, express js and tedious.
for more details about tedious go to tedious

/*
* Install below packages using node package manager
* 1) $ npm install express --save
* 2) $ npm install tedious --save
*
* Run Application using
* $ node index.js
/
                (function () {
    var express = require('express')
    var app = express()
    
    app.get('/', function (req, res) {
    
        // tedious package is used for SQL server connection
        var Connection = require('tedious').Connection;
        var Request = require('tedious').Request;
    
        // add SQL server configurations here
        var config = {
            server: '[server_name]',
            userName: '[user_name]',
            password: '[password]',        
            options: {            
                instanceName: '[instance_name]',
                database: '[database_name]'            
            }
        }
    
        var connection = new Connection(config);
    
        connection.on('connect', function (err,resp) {
            if (err) {
                console.log(err);
            } else {
                executeStatement(resp);            
            }
        });
    
        function executeStatement(resp) {
            // add SQL query here
            request = new Request('select * from [table_name] with (nolock)', function (err, rowCount) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(rowCount + ' rows');
                }
                connection.close();
                // return rowCount from API
                res.json(rowCount);
            });
    
            // row iteratore for result
            request.on('row', function (columns) {
                columns.forEach(function (column) {
                    if (column.value === null) {
                        console.log('NULL');
                    } else {
                        console.log(column.value);
                    }
                });
            });
            
            // execute query
            connection.execSql(request);        
        }
    });
    
    var server = app.listen(5000, function () {
        console.log('Server is running..');
    });
})();

Parent-Child class declaration and initialization

using System; namespace CSharpDemo {     public class A     {         public void print()         {             Console.Wr...