Monday, 16 August 2021

Linux CentOS - Enable Network Adaptor and Assign Static IP Address

To deploy OVA or OVF file, 

  • Login to VCenter with the Administrator account
  • Make sure to use FQDN in URL instead of IP Address
  • In case image file cannot be recognized by the version of VCenter, run command shell as admin and issue the following command
    • tar -xvf yourfilename.ova

Follow the link below to assign a static IP address to the network adaptor. CentOS may or maybe use eth0 as network adaptor name and could be something like: ens32

Use #ip a to see the network interfaces. more...

Monday, 9 August 2021

NodeJS and SQL

// npm install express --save
// npm install mssql --save
// npm install -g nodemon
// nodemon app.js

var express = require('express');
var sql = require('mssql');
var app = express();

app.use(express.json()); // parse json body

var _PROTOCOL = 'HTTP';
var _SERVER = 'localhost';
var _PORT = 4444;
var _DEFAULTPORT = 9000;

// config for database
var config = {
    user: '****',
    password: '****',
    server: 'localhost',
    database: 'mosis_portal',
    stream: false,
    options: {
        trustedConnection: true,
        encrypt: true,
        enableArithAbort: true,
        trustServerCertificate: true, // incase recieve error: self signed certificate
    }
};

app.get('/posts', function(req, res) {
    sql.connect(config, function(err) {
        if (err) console.log(err);
        var request = new sql.Request();
        request.query('select * from posts').then(recordset => {
            console.log(recordset.recordsets);
            res.status(200).send(recordset.recordsets);
        });
    });
});

app.get('/post/:id', function (req, res) {
    var id = req.params.id;
    sql.connect(config, function(err, result, fields) {
        if (err) console.log(err);
        var request = new sql.Request();
        request.query(`select * from posts where id= ${id}`).then(recordset => {
            res.status(200).send(recordset.recordsets.pop().pop());
        });
    });
});

app.get('/title/:id', function (req, res) {
    var id = req.params.id;
    sql.connect(config, function(err, result, fields) {
        if (err) console.log(err);
        var request = new sql.Request();
        request.query(`select top 1 * from posts where id= ${id}`).then(recordset => {
            var info = recordset.recordsets.pop().pop();
            var title = info.post_title;
            var id = info.id;
            console.log(info.id)
            res.status(200).send(`<h1 onclick="location.href='http://localhost:4444/post/${id}'">${title}</h1>`);
        });
    });
});

app.listen(_PORT || _DEFAULTPORT, function() {
    console.log(`\n\nServer running: ${_PROTOCOL}://${_SERVER}:${_PORT || _DEFAULTPORT}`);
});


Tuesday, 4 May 2021

SQL Recursion

-- Recursive SQL
WITH t(n) AS (
  SELECT 1
  UNION ALL
  SELECT n+1 FROM t WHERE n< 5
)

SELECT n
FROM t


-- Data Generation with Recursive SQL
WITH
    t1(v1, v2) AS (SELECT 1, 2),
    t2(w1, w2) AS (
        SELECT v1 * 2, v2 * 2
        FROM t1
)

SELECT *
FROM t1, t2


Thursday, 4 March 2021

Detect SQL Database Changes using C# Table Dependency and SQL Broker

using System;
using TableDependency.SqlClient;
using TableDependency.SqlClient.Base;
using TableDependency.SqlClient.Base.Enums;
using TableDependency.SqlClient.Base.EventArgs;

namespace SQLCDC
{
                class Program
                {
                                static void Main(string[] args)
                                {
                                                string connectionString = "Server=localhost;Database=Test;Integrated Security=True;";
                                                var mapper = new ModelToTableMapper<People>();
                                                mapper.AddMapping(c => c.name, "Name");
                                                mapper.AddMapping(c => c.age, "Age");
                                                using (var dep = new SqlTableDependency<People>(connectionString, "People", mapper: mapper))
                                                {
                                                                dep.OnChanged += Changed;
                                                                dep.Start();
                                                               Console.ReadKey();
                                                                dep.Stop();
                                                }
                                }

                                public static void Changed(object sender, RecordChangedEventArgs<People> e)
                                {
                                                var changedEntity = e.Entity;
                                                if (e.ChangeType != ChangeType.None)
                                                {
                                                                switch (e.ChangeType)
                                                                {
                                                                                case ChangeType.Delete:
                                                                                                break;
                                                                                case ChangeType.Update:
                                                                                               break;
                                                                                case ChangeType.Insert:
                                                                                               break;
                                                                                case ChangeType.None:
                                                                                                Console.WriteLine("None");
                                                                                                break;
                                                                }
                                                }
                                                Console.WriteLine("DML OPR: " + e.ChangeType);
                                                Console.WriteLine("   Name: " + changedEntity.name);
                                                Console.WriteLine("    Age: " + changedEntity.age);
                                                Console.WriteLine();
                                }
                }
                public class People
                {
                                public string name { get; set; }
                                public string age { get; set; }
                }
}

/*
                use master
                select is_broker_enabled from sys.databases where name= 'Test'

                use test
                go

                create table people (
                                id int identity (1,1) not null,
                                name varchar(50) not null,
                                age nchar(10),
                                primary key (id)
                )

                select * from people
                insert into people (name, age) values ('A', 45)
                insert into people (name, age) values ('B', 50)
                insert into people (name, age) values ('C', 23)

                select * from people
                update people set name='A1' where name='A' 

                -- truncate table people

*/ 

Read more...

Friday, 18 December 2020

Export data from stored procedure to CSV

 -- xp_cmdshell must be enabled under server security

DECLARE @cmd as varchar(4000) = 'BCP "Exec BikeStores.dbo.Test" QUERYOUT "D:\1.csv" -c -t\^, -T -S' + @@servername

EXEC master..xp_cmdshell @cmd



Friday, 4 September 2020

Powerwashing the Chromebook

  • Sign out of your Chromebook
  • Press and hold Ctrl + Alt + Shift + r
  • Select Restart
  • In the box that appears, select Powerwash. Continue
  • Follow the steps that appear and sign in with your Google Account


Analysis Service in Power BI Report Server

 We couldn’t connect to the Analysis Services server. Make sure you’ve entered the connection string correctly... link