/ Insights / Connect to Azure SQL Database with NodeJS and mssql Insights Connect to Azure SQL Database with NodeJS and mssql January 10, 2017This blog will show you how to use Node.js to connect to Azure SQL Database and perform read operations. Although there are several ways to connect to SQL Server from Node, the top two most popular packages are msnodesql and mssql.msnodesql – Microsoft’s SQL Server connector for Node.js. This enables Node.js apps on Windows and Azure platform to connect to SQL Server and Azure SQL.mssql – This a popular third-party SQL Server connector for Node.js that is very easy to use. This package supports Promises, Streams and callbacks to perform database operations. It also supports SQL statements, Stored Procedures, Transactions and Connection Pooling.We will be using mssql for our sample application. Before we get started creating our application, you need to have a Microsoft Azure account. If you don’t have an account, click here to get a free trial. Getting StartedUse Azure Management Portal to create a server and database.Sign into the Azure Management portalClick SQL databases from the left navigation menu.In the SQL Database blade, click Add at the top.Enter a Name for your Database.Use an existing Resource group, or create a new one.A Resource group is a collection of resources that share the same permissions and policies.Click Select Source to specify the source of the database.For our example, we will use the AdventureWorks sample databaseYou can also use a backup copy of an existing SQL database.Click Server, then Create a new server.Make sure the server name is uniqueEnter a Server Admin Login and Password.Choose a geographical location that’s close to youSelect no for SQL elastic pool.Click Pricing tier, then select the Basic option.Next, we will configure the firewall so the database can be accessed from a local client.Click SQL databases from the left navigation menu.Select the database we previously created.Click Set server firewall from the top menu.In the Firewall Settings blade, enter a Rule Name and paste in the your public IP address that is provided.We can now view the new SQL database within Visual Studio.Click Tools from the top menu of our database.Then select Open In Visual Studio.Once Visual Studio is open, select the AdventureWorks database from SQL Server Object Explorer menu.We will be using the Customer table for our sample application.Setup the Node.js appOpen the command prompt as administrator.Create a directory to store the Node project.Enter npm init -y inside the created directory to create a package.json file with default settings.Then enter npm install –save mssql to install the mssql package.Next, we will need to open the project in your favorite IDE or text editor. I will be using Atom.The CodeCreate a new file called index.js within the project.Then paste the following code with comments, and save the file.Brief explanation of the code:First we create a variable for the “mssql” npm package. This makes it easy to connect to SQL Server using Node.js.Then we create a configuration object for connecting to the Azure SQL database.Within the getCustomers() function, we use the “Connection” object to connect to SQL Server. Once connected, the “Request” object executes the SELECT statement and fetches the results.We also handle both SQL Server connection errors and SQL statement execution errors.Test in ConsoleWithin the command prompt run “Node index.js”.You should now see an array of Customer records within the console.ConclusionWe just built a simple Node.js app with an Azure SQL Database. The mssql package makes it pretty easy to connect SQL Server and Node.js.