electron js Tutorial

Electron JS How to Connect with SQL Server Database

In this article, we will learn How to Connect to SQL Server database using Electron JS.
In order to access SQL Server database using Electron JS we need to first download SQL Server drivers. We have multiple options for SQL Server drivers in npm. We will use mssql drivers for this article. We need to first download the Electron JS First Project files. We will use it as a starting point. Download and open the project in Visual Studio Code. Next, open terminal.

FileDownload
First Project.zipLogin to Download

Download mssql drivers

To download mssql drivers execute "npm install mssql" command in terminal.
Once the mssql drivers download completed.
Create a new file with name "SQLServer.js" and following code.

Code

function connectToDatabase(ID)
{
    var sql = require("mssql");
    // Database Configuration
    var config = {
        user: 'sa', 
        password: '[email protected]$$w0rd',
        server: 'localhost', 
        database: 'WideWorldImporters',
        options: {
            instanceName: 'DEV17'
            , encrypt: false
        }
    };
    
    // connect to your database
    sql.connect(config, function (err) {
        if (err) console.log(err);
        // create Request object
        var request = new sql.Request();
            
        // query to the database and get the records
        request.query('SELECT * FROM INFORMATION_SCHEMA.TABLES', function (err, recordset) {
            if (err) {
                console.log("Something went wrong")
            }
            else{
                
                //Conver Return Data Object to string
                var result = JSON.stringify(recordset);
                document.getElementById(ID).innerHTML=result;

            }
        });
    });
}

In this file, we have code to connect SQL Server database and then we are listing all the WideWorldImporters sample database tables. Let's add the new JS file in the index.html file. Next, we need to add a call to connectToDatabase function on the Windows Load event. This function is defined in SQLServer.js. Finally, we add a div element to display the output of connectToDatabase function. Following is the final version of the index.html file.

Code

<!DOCTYPE html>
  <html>
    <head>
      <meta charset="UTF-8">
      <title>Hello vLemonn!</title>
    </head>
    <body>
      <h1>Hello vLemonn!</h1>
      <p>
      We are using node <script>document.write(process.versions.node)</script>,
      Chrome <script>document.write(process.versions.chrome)</script>,
      and Electron <script>document.write(process.versions.electron)</script>,
      on OS <script>document.write(process.platform)</script>
    </p>
    <p>
        Hello everyone, This is <a href="https://mayanksanghvi.com">Mayank Sanghvi</a> 
        from <a href="https://vlemonn.com">vLemonn.com</a> 
        . This is our Electron JS tutorial Series. You can find complete Elecron JS Tutorial <a href="https://vlemonnc.com/tutorial/electon-js">here</a>
    </p>
    <p>
        <div id="dvResult"></div>
    </p>
    </body>
    <!-- SQL Servre Connection -->
    <script src="SQLServer.js"></script>
    <script >
        window.onload=function(e){
            connectToDatabase("dvResult")
        }
    </script>
  </html>

Execute our project. Following is the output for our demo app.

Finally, following are some different error messages.

Common Connection Errors and Resolutions

Error 1

ConnectionError: Failed to connect to localhost:undefined in 15000ms
    at Connection.tedious.once.err (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Connection.connectTimeout (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\connection.js:924:12)

Cause: SQL Server Browser services stopped or not running.

Resolution: Start SQL Server Browser Services.

Steps: Launch SQL Server Configuration Manager and start SQL Server Bowser Services. You can find SQL Server Manager Configuration location at the end of this article.

Error 2

ConnectionError: Port for DEV17 not found in localhost
    at Connection.tedious.once.err (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\connection.js:849:20
    at D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\instance-lookup.js:92:17
    at Socket.onMessage (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\sender.js:141:9)
    at emitTwo (events.js:126:13)
    at Socket.emit (events.js:214:7)
    at UDP.onMessage [as onmessage] (dgram.js:658:8)

Cause: SQL Server Browser Services are running but TCP/IP Protocol is Disabled

Resolution: Enable TCP/IP Protocol.

Steps: Launch SQL Server Configuration Manager and Enable TCP/IP Procol. You can find SQL Server Manager Configuration location at the end of this article.

Error 3

ConnectionError: Failed to lookup instance on WrongServer - getaddrinfo ENOTFOUND WrongServer
    at Connection.tedious.once.err (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\connection.js:849:20
    at D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\instance-lookup.js:83:15
    at GetAddrInfoReqWrap.callback (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\sender.js:58:18)
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:104:17)

Cause and Resolution: Wrong SQL Server Connection information. Please correct SQL Server Connection information. In this example we enter wrong server.

Error 4
ConnectionError: Port for DEV171 not found in localhost
    at Connection.tedious.once.err (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\connection.js:849:20
    at D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\instance-lookup.js:92:17
    at Socket.onMessage (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\sender.js:141:9)
    at emitTwo (events.js:126:13)
    at Socket.emit (events.js:214:7)
    at UDP.onMessage [as onmessage] (dgram.js:658:8)

Cause and Resolution: Wrong SQL Server Connection information. Please correct SQL Server Connection information. In this example we enter wrong instance name.

Error 4
ConnectionError: Login failed for user 'sa'.
    at Connection.tedious.once.err (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Connection.processLogin7Response (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\connection.js:1277:16)
    at Connection.message (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\connection.js:1789:14)
    at Connection.dispatchEvent (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\connection.js:992:38)
    at MessageIO.<anonymous> (D:\Documents\Other\Data\Tutorial\Development\Desktop\Electron\How2\SQL Server Connection\node_modules\tedious\lib\connection.js:886:18)
    at emitNone (events.js:106:13)
    at MessageIO.emit (events.js:208:7)

Cause and Resolution: Wrong SQL Server Connection information. Please correct SQL Server Connection information. In this example we enter wrong password.

SQL Server Configuration Manager Locations

SQL Server 2017: C:\Windows\SysWOW64\SQLServerManager14.msc
SQL Server 2016: C:\Windows\SysWOW64\SQLServerManager13.msc
SQL Server 2014 (12.x): C:\Windows\SysWOW64\SQLServerManager12.msc
SQL Server 2012 (11.x): C:\Windows\SysWOW64\SQLServerManager11.msc

...

About The Author

Hello, I am Mayank Sanghvi I am primarily a BI developer with experience in MSBI and Cognos BI Suit. Also having interest in various other technologies such as Android, Java, C#, ASP .NET and other. I enjoy learning new technologies and share my experience with others.

0 Comments
Leave A Comment

Please login to post your valuable comments.

add