Writing data to Microsoft SQL Server from web browser using REST API and Node.js

In previous blog post Creating REST API for reading data from Microsoft SQL Server in web browser

we have looked into creating REST API calls for reading data from SQL Server Database. And we also looked into all the steps you should take to do all the installation. In this blogpost, we will do the reverse. Push the data into the SQL Server database using REST API that will result in INSERT statement (or UPDATE statement) on the database.

For this scenario, we will use two files (both available on Github):
1) Config.js (which remained the same and untouched from previous blog post)
2) WriteApp.js

The Content of WriteApp.js should be:

// Read functions
const express = require('express'); 
const app = express();
const sql = require('mssql/msnodesqlv8') //mssql with MS driver for SQL Server
var beautify = require("json-beautify");
 
var env = process.env.NODE_ENV || 'production';
var sqlConfig = require('./config')[env];

const path = require('path')


// View Engine Setup
app.set("views", path.join(__dirname))
app.set("view engine", "ejs")
  
 
// Start server and listen on http://localhost:2908/
var server = app.listen(2908, function() {
  var host = server.address().address
  var port = server.address().port
 
  console.log("app listening at http://%s:%s", host, port)
});
 
const connection = new sql.ConnectionPool(sqlConfig, function(err){
      if (err){
      console.log(err);
      }
    }
)

// Sample with mixed input parameters. Case sensitive and you can use special chars + space breaks
//Use URL: http://localhost:2908/insert/UsersAD?EmloyeeID=222&SamAccountName=tomazt&DisplayName=tomaz tom&Email=tt@gmail.com


 app.get('/insert/UsersAD', function(req,res){

   var EmloyeeID = req.query.EmloyeeID;
   var SamAccountName = req.query.SamAccountName;
   var DisplayName = req.query.DisplayName;
   var Email = req.query.Email;

   console.log("Value for EmployeeID: ",EmloyeeID);
   console.log("Value for SamAccountName: ",SamAccountName);
   console.log("Value for DisplayName: ",DisplayName);
   console.log("Value for Email: ",Email);

  
 connection.connect(function(err) {
 connection.query("INSERT INTO dbo.UsersAD (EmloyeeID, SamAccountName,DisplayName, Email) values ("+Number(EmloyeeID)+",'"+String(SamAccountName)+"', '"+String(DisplayName)+"', '"+String(Email)+"')",function(err,result){

 if(!!err){
 console.log(err);
 res.send('Error in inserting');
 }
 else{
   res.send('Successfully Insertion');
 }});});

 });

This file will send the information to the same SQL table, that we have created in previous blog post.

USE APITest;

CREATE TABLE dbo.UsersAD
( ID INT IDENTITY(1,1) NOT NULL
,EmloyeeID INT 
,SamAccountName VARCHAR(100)
,DisplayName VARCHAR(200)
,Email VARCHAR(100)
)


INSERT INTO dbo.UsersAD (EmloyeeID, SamAccountName, DisplayName, Email)
          SELECT 21,'MichelH','Michel Houell','michelh@account.com'
UNION ALL SELECT 22,'NielT','Niel Ty','NielT@account.com'
UNION ALL SELECT 25,'ImmanuelK','Immanuel Kan','ImmanuelK@account.com'
UNION ALL SELECT 30,'BillG','Bill William Gate','BillG@account.com'

Once you have changed and save both javascript files, you can now run the node application. In the same folder (SQLAPI) run the following command:

node WriteApp.js

And open the URL location in your browser type:
http://localhost:2908/insert/UsersAD?EmloyeeID=222&SamAccountName=tomazt&DisplayName=tomaz tom&Email=tt@tomaztsql

And you should receive a message in your browser:

In the background, a new record should be inserted in the database – emloyeeID = 222

And the result of log should also be printed in the command log:

Besides the INSERT command, you can do any CRUD statements, but we should be cautions about that.

In next blog post, we will look into the ability to do the Predictions with R model and SQL Server using API.

As always, all the code is available on Github – tomaztk/MSSQLServerRestAPI.

Tagged with: , , , , ,
Posted in Uncategorized
One comment on “Writing data to Microsoft SQL Server from web browser using REST API and Node.js
  1. […] Writing DAta to Microsoft SQL Server from web browser using REST API and node.js […]

    Liked by 1 person

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...