<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Microsoft SQL Server client for Node JS &#8211; Sibeesh Passion</title>
	<atom:link href="https://sibeeshpassion.com/tag/microsoft-sql-server-client-for-node-js/feed/" rel="self" type="application/rss+xml" />
	<link>https://sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Tue, 10 Jul 2018 07:46:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Microsoft SQL Server client for Node JS &#8211; Sibeesh Passion</title>
	<link>https://sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>SQL Server CRUD Actions Using Node JS</title>
		<link>https://sibeeshpassion.com/sql-server-crud-actions-using-node-js/</link>
					<comments>https://sibeeshpassion.com/sql-server-crud-actions-using-node-js/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sun, 27 Nov 2016 16:40:21 +0000</pubDate>
				<category><![CDATA[Node JS]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[CRUD in Node JS]]></category>
		<category><![CDATA[Database actions using Node JS]]></category>
		<category><![CDATA[Microsoft SQL Server client for Node JS]]></category>
		<category><![CDATA[MSSQL Node JS]]></category>
		<category><![CDATA[Node JS Application In Visual Studio]]></category>
		<category><![CDATA[SQL Server For Node JS]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11947</guid>

					<description><![CDATA[[toc] Introduction In this post we will see how we can perform CRUD application in our SQL database using Node JS. As you all know Node JS is a run time environment built on Chrome&#8217;s V8 JavaScript engine for server side and networking application. And it is an open source which supports cross platforms. Node JS applications are written in pure JavaScript. If you are new to Node JS, I strongly recommend you to read my previous post about Node JS here. Now let&#8217;s begin. Download source code SQL Server CRUD Actions Using Node JS Background There was a time [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>In this post we will see how we can perform CRUD application in our <a href="http://sibeeshpassion.com/category/SQL" target="_blank" rel="noopener">SQL </a> database using <a href="http://sibeeshpassion.com/category/Node-JS/" target="_blank" rel="noopener">Node JS</a>. As you all know Node JS is a run time environment built on Chrome&#8217;s V8 JavaScript engine for server side and networking application. And it is an open source which supports cross platforms. Node JS applications are written in pure <a href="http://sibeeshpassion.com/category/javascript/" target="_blank" rel="noopener">JavaScript</a>. If you are new to Node JS, I strongly recommend you to read my previous post about Node JS <a href="http://sibeeshpassion.com/introduction-to-node-js/" target="_blank" rel="noopener">here</a>. Now let&#8217;s begin.</p>
<h3><strong>Download source code</strong></h3>
<ul>
<li><a href="https://code.msdn.microsoft.com/SQL-Server-CRUD-Actions-6bc910fd" target="_blank" rel="noopener">SQL Server CRUD Actions Using Node JS </a></li>
</ul>
<h3><strong>Background</strong></h3>
<p>There was a time where we developers are depended on any server side languages to perform server side actions, few years back a company called Joyent, Inc brought us a solution for this. That is, we can do the server side actions if you know JavaScript. Because of the wonderful idea behind this, it became a great success. You can do server side actions without knowing a single code related to any server side languages like <a href="http://sibeeshpassion.com/category/csharp/" target="_blank" rel="noopener">C#</a> and <a href="http://sibeeshpassion.com/category/PHP/" target="_blank" rel="noopener">PHP</a>. Here we are going to see how you can do the database actions like Create, Read, Update, Delete using Node JS. I hope you will like this.</p>
<p>Before we start coding our Node JS application, we can set up Node JS tool available for <a href="http://sibeeshpassion.com/category/visual-studio/" target="_blank" rel="noopener">Visual Studio</a>.</p>
<h3><strong>Node JS tool for Visual Studio</strong></h3>
<blockquote><p>You can always run your Node JS code by using a command prompt, so setting up this tool is optional. If you install it, you can easily debug and develop Node JS. So I recommend you to install it.</p></blockquote>
<p>To download the tool, please click on this <a href="https://www.visualstudio.com/vs/node-js/" target="_blank" rel="noopener">link</a>. Once you have downloaded the set up file, you can start installing it.</p>
<div id="attachment_11948" style="width: 628px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_tool_for_Visual_Studio.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-11948" class="size-full wp-image-11948" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_tool_for_Visual_Studio.png" alt="node_js_tool_for_visual_studio" width="618" height="483" srcset="/wp-content/uploads/2016/11/Node_JS_tool_for_Visual_Studio.png 618w, /wp-content/uploads/2016/11/Node_JS_tool_for_Visual_Studio-300x234.png 300w, /wp-content/uploads/2016/11/Node_JS_tool_for_Visual_Studio-400x313.png 400w" sizes="(max-width: 618px) 100vw, 618px" /></a><p id="caption-attachment-11948" class="wp-caption-text">node_js_tool_for_visual_studio</p></div>
<p>So I hope you have installed the application, Now you can create a Node JS application in our Visual Studio.</p>
<h3><strong>Creating Node JS Application In Visual Studio</strong></h3>
<p>You can find an option as Node JS in your Add New Project window as follows. Please click on that and create a new project.</p>
<div id="attachment_11949" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/New_Node_JS_Project.png"><img decoding="async" aria-describedby="caption-attachment-11949" class="size-large wp-image-11949" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/New_Node_JS_Project-1024x709.png" alt="new_node_js_project" width="634" height="439" srcset="/wp-content/uploads/2016/11/New_Node_JS_Project-1024x709.png 1024w, /wp-content/uploads/2016/11/New_Node_JS_Project-300x208.png 300w, /wp-content/uploads/2016/11/New_Node_JS_Project-768x532.png 768w, /wp-content/uploads/2016/11/New_Node_JS_Project-160x110.png 160w, /wp-content/uploads/2016/11/New_Node_JS_Project-400x277.png 400w, /wp-content/uploads/2016/11/New_Node_JS_Project-866x600.png 866w, /wp-content/uploads/2016/11/New_Node_JS_Project.png 515w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11949" class="wp-caption-text">new_node_js_project</p></div>
<p>Now our Visual Studio is ready for coding, but as I mentioned earlier, we are going to use <a href="http://sibeeshpassion.com/category/sql-server/" target="_blank" rel="noopener">SQL Server</a> as our database. So we need to do some configuration related to that too. Let&#8217;s do it now.</p>
<h4><strong>Configure SQL Server For Node JS Development</strong></h4>
<p>You need to make sure that the following service are run.</p>
<ul>
<li>SQL Server</li>
<li>SQL Server Agent (Skip it if you are using SQLEXPRESS</li>
<li>SQL Server Browser</li>
</ul>
<p>To check the status of these service, you can always services by running <em>services.msc</em> in Run command window. Once you are done, you need to enables some protocols and assign a port to it. Now go to your SQL Server Configuration Manager. Most probably you can find the file in this <em>C:\Windows\SysWOW64</em> location, if you cant find it start window.</p>
<div id="attachment_11950" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/SQL_Server_Manager_Location-e1480260854402.png"><img decoding="async" aria-describedby="caption-attachment-11950" class="size-full wp-image-11950" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/SQL_Server_Manager_Location-e1480260854402.png" alt="sql_server_manager_location" width="634" height="206" srcset="/wp-content/uploads/2016/11/SQL_Server_Manager_Location-e1480260854402.png 634w, /wp-content/uploads/2016/11/SQL_Server_Manager_Location-e1480260854402-300x97.png 300w, /wp-content/uploads/2016/11/SQL_Server_Manager_Location-e1480260854402-400x130.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11950" class="wp-caption-text">sql_server_manager_location</p></div>
<p>Now go to SQL Server Network Configuration and click on Protocols for SQLEXPRESS(Your SQL Server) and Enable TCP/IP.</p>
<div id="attachment_11951" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Protocols_For_SQL_EXPRESS-e1480261046507.png"><img decoding="async" aria-describedby="caption-attachment-11951" class="size-full wp-image-11951" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Protocols_For_SQL_EXPRESS-e1480261046507.png" alt="protocols_for_sql_express" width="634" height="266" srcset="/wp-content/uploads/2016/11/Protocols_For_SQL_EXPRESS-e1480261046507.png 634w, /wp-content/uploads/2016/11/Protocols_For_SQL_EXPRESS-e1480261046507-300x126.png 300w, /wp-content/uploads/2016/11/Protocols_For_SQL_EXPRESS-e1480261046507-400x168.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11951" class="wp-caption-text">protocols_for_sql_express</p></div>
<p>Now right click and click on Properties on TCP/IP. Go to to IP Addresses and assign port for all the IP.</p>
<div id="attachment_11952" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server.png"><img decoding="async" aria-describedby="caption-attachment-11952" class="size-large wp-image-11952" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server-1024x707.png" alt="assigning_tcp_ip_port_in_sql_server" width="634" height="438" srcset="/wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server-1024x707.png 1024w, /wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server-300x207.png 300w, /wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server-768x530.png 768w, /wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server-160x110.png 160w, /wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server-400x276.png 400w, /wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server-869x600.png 869w, /wp-content/uploads/2016/11/Assigning_TCP_IP_Port_In_SQL_Server.png 517w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11952" class="wp-caption-text">assigning_tcp_ip_port_in_sql_server</p></div>
<p>If have done it, it is time to set up our database and insert some data. Please do not forget to restart your service, as it is mandatory to get updated the changes we have done in the configurations.</p>
<div id="attachment_11954" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Restart_SQL_EXPRESS.png"><img decoding="async" aria-describedby="caption-attachment-11954" class="size-large wp-image-11954" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Restart_SQL_EXPRESS-1024x595.png" alt="restart_sql_express" width="634" height="368" srcset="/wp-content/uploads/2016/11/Restart_SQL_EXPRESS-1024x595.png 1024w, /wp-content/uploads/2016/11/Restart_SQL_EXPRESS-300x174.png 300w, /wp-content/uploads/2016/11/Restart_SQL_EXPRESS-768x446.png 768w, /wp-content/uploads/2016/11/Restart_SQL_EXPRESS-400x233.png 400w, /wp-content/uploads/2016/11/Restart_SQL_EXPRESS-1032x600.png 1032w, /wp-content/uploads/2016/11/Restart_SQL_EXPRESS.png 614w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11954" class="wp-caption-text">restart_sql_express</p></div>
<h4><strong>Creating database</strong></h4>
<p>Here I am creating a database with name &#8220;TrialDB&#8221;, you can always create a DB by running the query given below.</p>
<p>[sql]<br />
USE [master]<br />
GO</p>
<p>/****** Object: Database [TrialDB] Script Date: 20-11-2016 03:54:53 PM ******/<br />
CREATE DATABASE [TrialDB]<br />
CONTAINMENT = NONE<br />
ON PRIMARY<br />
( NAME = N&#8217;TrialDB&#8217;, FILENAME = N&#8217;C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\TrialDB.mdf&#8217; , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )<br />
LOG ON<br />
( NAME = N&#8217;TrialDB_log&#8217;, FILENAME = N&#8217;C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\TrialDB_log.ldf&#8217; , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET COMPATIBILITY_LEVEL = 130<br />
GO</p>
<p>IF (1 = FULLTEXTSERVICEPROPERTY(&#8216;IsFullTextInstalled&#8217;))<br />
begin<br />
EXEC [TrialDB].[dbo].[sp_fulltext_database] @action = &#8216;enable&#8217;<br />
end<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET ANSI_NULL_DEFAULT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET ANSI_NULLS OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET ANSI_PADDING OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET ANSI_WARNINGS OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET ARITHABORT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET AUTO_CLOSE OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET AUTO_SHRINK OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET AUTO_UPDATE_STATISTICS ON<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET CURSOR_CLOSE_ON_COMMIT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET CURSOR_DEFAULT GLOBAL<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET CONCAT_NULL_YIELDS_NULL OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET NUMERIC_ROUNDABORT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET QUOTED_IDENTIFIER OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET RECURSIVE_TRIGGERS OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET DISABLE_BROKER<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET DATE_CORRELATION_OPTIMIZATION OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET TRUSTWORTHY OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET ALLOW_SNAPSHOT_ISOLATION OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET PARAMETERIZATION SIMPLE<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET READ_COMMITTED_SNAPSHOT OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET HONOR_BROKER_PRIORITY OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET RECOVERY SIMPLE<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET MULTI_USER<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET PAGE_VERIFY CHECKSUM<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET DB_CHAINING OFF<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET TARGET_RECOVERY_TIME = 60 SECONDS<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET DELAYED_DURABILITY = DISABLED<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET QUERY_STORE = OFF<br />
GO</p>
<p>USE [TrialDB]<br />
GO</p>
<p>ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;<br />
GO</p>
<p>ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;<br />
GO</p>
<p>ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;<br />
GO</p>
<p>ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;<br />
GO</p>
<p>ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;<br />
GO</p>
<p>ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;<br />
GO</p>
<p>ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;<br />
GO</p>
<p>ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;<br />
GO</p>
<p>ALTER DATABASE [TrialDB] SET READ_WRITE<br />
GO<br />
[/sql]</p>
<p><strong>Create a table and insert data in database</strong></p>
<p>To create a table, you can run the query below.</p>
<p>[sql]<br />
USE [TrialDB]<br />
GO</p>
<p>/****** Object: Table [dbo].[Course] Script Date: 20-11-2016 03:57:30 PM ******/<br />
SET ANSI_NULLS ON<br />
GO</p>
<p>SET QUOTED_IDENTIFIER ON<br />
GO</p>
<p>CREATE TABLE [dbo].[Course](<br />
[CourseID] [int] NOT NULL,<br />
[CourseName] [nvarchar](50) NOT NULL,<br />
[CourseDescription] [nvarchar](100) NULL,<br />
CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED<br />
(<br />
[CourseID] ASC<br />
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />
) ON [PRIMARY]</p>
<p>GO<br />
[/sql]</p>
<p>Now we can insert few data to our newly created table.</p>
<p>[sql]<br />
USE [TrialDB]<br />
GO</p>
<p>INSERT INTO [dbo].[Course]<br />
([CourseID]<br />
,[CourseName]<br />
,[CourseDescription])<br />
VALUES<br />
(1<br />
,&#8217;C#&#8217;<br />
,&#8217;Learn C# in 7 days&#8217;)<br />
INSERT INTO [dbo].[Course]<br />
([CourseID]<br />
,[CourseName]<br />
,[CourseDescription])<br />
VALUES<br />
(2<br />
,&#8217;Asp.Net&#8217;<br />
,&#8217;Learn Asp.Net in 7 days&#8217;)<br />
INSERT INTO [dbo].[Course]<br />
([CourseID]<br />
,[CourseName]<br />
,[CourseDescription])<br />
VALUES<br />
(3<br />
,&#8217;SQL&#8217;<br />
,&#8217;Learn SQL in 7 days&#8217;)<br />
INSERT INTO [dbo].[Course]<br />
([CourseID]<br />
,[CourseName]<br />
,[CourseDescription])<br />
VALUES<br />
(4<br />
,&#8217;JavaScript&#8217;<br />
,&#8217;Learn JavaScript in 7 days&#8217;)<br />
GO<br />
[/sql]</p>
<p>So our data is ready, that means we are all set to write our Node JS application. Go to the application we created and you can see a JS file there, normally named as <em>server.js</em>. Here I am going to change the name as <em>App.js</em>.</p>
<h4><strong>MSSQL &#8211; Microsoft SQL Server client for Node.js</strong></h4>
<p>You can find many packages for our day to day life in Node JS, what you need to do all is , just install that package and start using it. Here we are going to use a package called MSSQL.</p>
<p>Node-MSSQL</p>
<ul>
<li>Has unified interface for multiple TDS drivers.</li>
<li>Has built-in connection pooling.</li>
<li>Supports built-in JSON serialization introduced in SQL Server 2016.</li>
<li>Supports Stored Procedures, Transactions, Prepared Statements, Bulk Load and TVP.</li>
<li>Supports serialization of Geography and Geometry CLR types.</li>
<li>Has smart JS data type to SQL data type mapper.</li>
<li>Supports Promises, Streams and standard callbacks.</li>
<li>Supports ES6 tagged template literals.</li>
<li>Is stable and tested in production environment.</li>
<li>Is well documented.</li>
</ul>
<p>You can find more about the package <a href="https://www.npmjs.com/package/mssql" target="_blank" rel="noopener">here</a>. You can easily install this package by running the following command in your Nuget Package Manager Console.</p>
<p>[csharp]<br />
npm install mssql<br />
[/csharp]</p>
<div id="attachment_11953" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/MSSQL_Node_JS_Install.png"><img decoding="async" aria-describedby="caption-attachment-11953" class="size-large wp-image-11953" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/MSSQL_Node_JS_Install-1024x236.png" alt="mssql_node_js_install" width="634" height="146" srcset="/wp-content/uploads/2016/11/MSSQL_Node_JS_Install-1024x236.png 1024w, /wp-content/uploads/2016/11/MSSQL_Node_JS_Install-300x69.png 300w, /wp-content/uploads/2016/11/MSSQL_Node_JS_Install-768x177.png 768w, /wp-content/uploads/2016/11/MSSQL_Node_JS_Install-400x92.png 400w, /wp-content/uploads/2016/11/MSSQL_Node_JS_Install.png 1466w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11953" class="wp-caption-text">mssql_node_js_install</p></div>
<p>Now we can load this package by using a function called require.</p>
<p>[js]<br />
//MSSQL Instance Creation<br />
var sqlInstance = require(&#8220;mssql&#8221;);<br />
[/js]</p>
<p>Then, you can set the database configurations as preceding.</p>
<p>[js]<br />
/Database configuration<br />
var setUp = {<br />
server: &#8216;localhost&#8217;,<br />
database: &#8216;TrialDB&#8217;,<br />
user: &#8216;sa&#8217;,<br />
password: &#8216;sa&#8217;,<br />
port: 1433<br />
};<br />
[/js]</p>
<p>Once you have a configuration set up, you can connect your database by using connect() function.</p>
<p>[js]<br />
sqlInstance.connect(setUp)<br />
[/js]</p>
<p>Now we can perform the CRUD operations. Are you ready?</p>
<h4><strong>Select all the data from database using Node JS</strong></h4>
<p>[js]<br />
// To retrieve all the data &#8211; Start<br />
new sqlInstance.Request()<br />
.query(&#8220;select * from Course&#8221;)<br />
.then(function (dbData) {<br />
if (dbData == null || dbData.length === 0)<br />
return;<br />
console.dir(&#8216;All the courses&#8217;);<br />
console.dir(dbData);<br />
})<br />
.catch(function (error) {<br />
console.dir(error);<br />
});</p>
<p>// To retrieve all the data &#8211; End<br />
[/js]</p>
<p>Now run your application and see the output as preceding.</p>
<div id="attachment_11955" style="width: 593px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Select_All_Data_From_Database.png"><img decoding="async" aria-describedby="caption-attachment-11955" class="size-full wp-image-11955" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Select_All_Data_From_Database.png" alt="node_js_select_all_data_from_database" width="583" height="414" srcset="/wp-content/uploads/2016/11/Node_JS_Select_All_Data_From_Database.png 583w, /wp-content/uploads/2016/11/Node_JS_Select_All_Data_From_Database-300x213.png 300w, /wp-content/uploads/2016/11/Node_JS_Select_All_Data_From_Database-400x284.png 400w" sizes="(max-width: 583px) 100vw, 583px" /></a><p id="caption-attachment-11955" class="wp-caption-text">node_js_select_all_data_from_database</p></div>
<h4><strong>Select data with where condition from database using Node JS</strong></h4>
<p>You can always select a particular record by giving an appropriate select query as follows.</p>
<p>[js]<br />
// To retrieve specicfic data &#8211; Start<br />
var value = 2;<br />
new sqlInstance.Request()<br />
.input(&#8220;param&#8221;, sqlInstance.Int, value)<br />
.query(&#8220;select * from Course where CourseID = @param&#8221;)<br />
.then(function (dbData) {<br />
if (dbData == null || dbData.length === 0)<br />
return;<br />
console.dir(&#8216;Course with ID = 2&#8217;);<br />
console.dir(dbData);<br />
})<br />
.catch(function (error) {<br />
console.dir(error);<br />
});<br />
// To retrieve specicfic data &#8211; End<br />
[/js]</p>
<p>So what would be the output of the above code? Any idea?</p>
<div id="attachment_11956" style="width: 593px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Select_Particular_Data_From_Database.png"><img decoding="async" aria-describedby="caption-attachment-11956" class="size-full wp-image-11956" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Select_Particular_Data_From_Database.png" alt="node_js_select_particular_data_from_database" width="583" height="414" srcset="/wp-content/uploads/2016/11/Node_JS_Select_Particular_Data_From_Database.png 583w, /wp-content/uploads/2016/11/Node_JS_Select_Particular_Data_From_Database-300x213.png 300w, /wp-content/uploads/2016/11/Node_JS_Select_Particular_Data_From_Database-400x284.png 400w" sizes="(max-width: 583px) 100vw, 583px" /></a><p id="caption-attachment-11956" class="wp-caption-text">node_js_select_particular_data_from_database</p></div>
<h4><strong>Insert data to database using Node JS</strong></h4>
<p>We can always perform some insert query too using Node JS, the difference will be here is, as we have Transactions in SQL we will include that too here. Following code performs an insert operation.</p>
<p>[js]<br />
// Insert data &#8211; Start<br />
var dbConn = new sqlInstance.Connection(setUp,<br />
function (err) {<br />
var myTransaction = new sqlInstance.Transaction(dbConn);<br />
myTransaction.begin(function (error) {<br />
var rollBack = false;<br />
myTransaction.on(&#8216;rollback&#8217;,<br />
function (aborted) {<br />
rollBack = true;<br />
});<br />
new sqlInstance.Request(myTransaction)<br />
.query(&#8220;INSERT INTO [dbo].[Course] ([CourseName],[CourseDescription]) VALUES (&#8216;Node js&#8217;, &#8216;Learn Node JS in 7 days&#8217;)&#8221;,<br />
function (err, recordset) {<br />
if (err) {<br />
if (!rollBack) {<br />
myTransaction.rollback(function (err) {<br />
console.dir(err);<br />
});<br />
}<br />
} else {<br />
myTransaction.commit().then(function (recordset) {<br />
console.dir(&#8216;Data is inserted successfully!&#8217;);<br />
}).catch(function (err) {<br />
console.dir(&#8216;Error in transaction commit &#8216; + err);<br />
});<br />
}<br />
});<br />
});<br />
});<br />
// Insert data &#8211; End<br />
[/js]</p>
<p>So let&#8217;s run it and see the output.</p>
<div id="attachment_11957" style="width: 593px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Insert_Data_To_Database.png"><img decoding="async" aria-describedby="caption-attachment-11957" class="size-full wp-image-11957" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Insert_Data_To_Database.png" alt="node_js_insert_data_to_database" width="583" height="414" srcset="/wp-content/uploads/2016/11/Node_JS_Insert_Data_To_Database.png 583w, /wp-content/uploads/2016/11/Node_JS_Insert_Data_To_Database-300x213.png 300w, /wp-content/uploads/2016/11/Node_JS_Insert_Data_To_Database-400x284.png 400w" sizes="(max-width: 583px) 100vw, 583px" /></a><p id="caption-attachment-11957" class="wp-caption-text">node_js_insert_data_to_database</p></div>
<h4><strong>Delete data from database using Node JS</strong></h4>
<p>As we performed insert operation, we can do the same for delete operation as below.</p>
<p>[js]<br />
// Delete data &#8211; Start<br />
var delValue = 4;<br />
var dbConn = new sqlInstance.Connection(setUp,<br />
function (err) {<br />
var myTransaction = new sqlInstance.Transaction(dbConn);<br />
myTransaction.begin(function (error) {<br />
var rollBack = false;<br />
myTransaction.on(&#8216;rollback&#8217;,<br />
function (aborted) {<br />
rollBack = true;<br />
});<br />
new sqlInstance.Request(myTransaction)<br />
.query(&#8220;DELETE FROM [dbo].[Course] WHERE CourseID=&#8221; + delValue,<br />
function (err, recordset) {<br />
if (err) {<br />
if (!rollBack) {<br />
myTransaction.rollback(function (err) {<br />
console.dir(err);<br />
});<br />
}<br />
} else {<br />
myTransaction.commit().then(function (recordset) {<br />
console.dir(&#8216;Data is deleted successfully!&#8217;);<br />
}).catch(function (err) {<br />
console.dir(&#8216;Error in transaction commit &#8216; + err);<br />
});<br />
}<br />
});<br />
});<br />
});<br />
// Delete data &#8211; End<br />
[/js]</p>
<p>Now run your application and see whether the data is deleted.</p>
<div id="attachment_11958" style="width: 593px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Delete_Data_From_Database.png"><img decoding="async" aria-describedby="caption-attachment-11958" class="size-full wp-image-11958" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Delete_Data_From_Database.png" alt="node_js_delete_data_from_database" width="583" height="414" srcset="/wp-content/uploads/2016/11/Node_JS_Delete_Data_From_Database.png 583w, /wp-content/uploads/2016/11/Node_JS_Delete_Data_From_Database-300x213.png 300w, /wp-content/uploads/2016/11/Node_JS_Delete_Data_From_Database-400x284.png 400w" sizes="(max-width: 583px) 100vw, 583px" /></a><p id="caption-attachment-11958" class="wp-caption-text">node_js_delete_data_from_database</p></div>
<h4><strong>Update data from database using Node JS</strong></h4>
<p>The only action pending here to perform is UPDATE. Am I right? Let&#8217;s do that too.</p>
<p>[js]<br />
// Update data &#8211; Start<br />
var updValue = 3;<br />
var dbConn = new sqlInstance.Connection(setUp,<br />
function (err) {<br />
var myTransaction = new sqlInstance.Transaction(dbConn);<br />
myTransaction.begin(function (error) {<br />
var rollBack = false;<br />
myTransaction.on(&#8216;rollback&#8217;,<br />
function (aborted) {<br />
rollBack = true;<br />
});<br />
new sqlInstance.Request(myTransaction)<br />
.query(&#8220;UPDATE [dbo].[Course] SET [CourseName] = &#8216;Test&#8217; WHERE CourseID=&#8221; + updValue,<br />
function (err, recordset) {<br />
if (err) {<br />
if (!rollBack) {<br />
myTransaction.rollback(function (err) {<br />
console.dir(err);<br />
});<br />
}<br />
} else {<br />
myTransaction.commit().then(function (recordset) {<br />
console.dir(&#8216;Data is updated successfully!&#8217;);<br />
}).catch(function (err) {<br />
console.dir(&#8216;Error in transaction commit &#8216; + err);<br />
});<br />
}<br />
});<br />
});<br />
});<br />
// Update data &#8211; End<br />
[/js]</p>
<p>Here goes the output.</p>
<div id="attachment_11959" style="width: 593px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Update_Data_From_Database.png"><img decoding="async" aria-describedby="caption-attachment-11959" class="size-full wp-image-11959" src="http://sibeeshpassion.com/wp-content/uploads/2016/11/Node_JS_Update_Data_From_Database.png" alt="node_js_update_data_from_database" width="583" height="414" srcset="/wp-content/uploads/2016/11/Node_JS_Update_Data_From_Database.png 583w, /wp-content/uploads/2016/11/Node_JS_Update_Data_From_Database-300x213.png 300w, /wp-content/uploads/2016/11/Node_JS_Update_Data_From_Database-400x284.png 400w" sizes="(max-width: 583px) 100vw, 583px" /></a><p id="caption-attachment-11959" class="wp-caption-text">node_js_update_data_from_database</p></div>
<p>You can always download the source code attached to see the complete code and application. Happy coding!.</p>
<h3><strong>See also</strong></h3>
<ul>
<li><a href="https://www.npmjs.com/package/mssql" target="_blank" rel="noopener">MSSQL Package for Node JS</a></li>
<li><a href="https://nodejs.org/en/" target="_blank" rel="noopener">Node js</a></li>
</ul>
<h3><strong>Conclusion</strong></h3>
<p>Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h3><strong>Your turn. What do you think?</strong></h3>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sibeeshpassion.com/sql-server-crud-actions-using-node-js/feed/</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
	</channel>
</rss>
