Organize/Add Data into SQL Server Table using Visual Studio 2019 Node.js

0

So I was able to connect Node.js to SQL server and get a (poorly organized) display of data from a SQL Server database table onto a localhost in chrome.

That was just the connection, now I'm trying to run operations and edit data into the db table through Node.js in Visual Studio 2019

So this part of the code works.

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

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

    var sql = require('mssql');

    //config for db
    var dbConfig = {
        server: 'localhost',
        database: 'blah',
        user: 'sa',
        password: 'blah'
    };

    //connect to db
    sql.connect(dbConfig, 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 Clients', function (err, recordset) {

            if (err) console.log(err)

            // send records as a response
            res.send(recordset);

        });
    });
});


var server = app.listen(5000, function () {
    console.log('Server is running..');
});

It opens a localhost tab in chrome and just basically spits out the columns and data from the table.

This is the code I added

function LoadClients() {
    var dbConn = new sql.Connection(config);
    dbConn.connect().then(function () {
        var request = new sql.Request(dbConn);
        request.query('Select * from Clients').then(function (recordSet) {
            console.log(recordSet);
            dbConn.close();
        }).catch(function (err) {
            console.log(err);
            dbConn.close();
        });
    }).catch(function (err) {
        console.log(err);
    });
}
LoadClients();

function AddClient() {
    var dbConn = new sql.Connection(config);
        dbConn.connect().then(function () {
            var addclient = new.sql.AddClient(dbConn);
            addclient.begin().then(function () {
                var request = new sql.Request(addclient);
                request.query('Insert into Clients (first, last, middle) values ('FirstTest' , 'LastTest' , 'MiddleTest') ')
                    .then(function () {

                        addclient.commit().then(function (recordSet) {
                            console.log(recordSet);
                            dbConn.close();
                        }).catch(function (err) {
                            console.log("Error in AddClient Commit " + err);
                            dbConn.close();
                        });
                    }).catch(function (err) {
                        console.log("Error in AddClient Begin " + err);
                        dbConn.close();
                    });

            }).catch(function (err) {
                console.log(err);
                dbConn.close();
            });
        }).catch(function (err) {
            console.log(err);
        });
}
AddClient();
}

Error message "The program 'Server.js' has exited with code -1 (0xffffffff)."

Last time it was a misspelling or having a capitalized letter on a variable that caused the error, but I triple-checked the code to make sure the variables are in place.

I expect to have the Load Clients function replace

    //connect to db
    sql.connect(dbConfig, 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 Clients', function (err, recordset) {

            if (err) console.log(err)

            // send records as a response
            res.send(recordset);

        });
    });
});


var server = app.listen(5000, function () {
    console.log('Server is running..');
});

as well as have the query request in AddClient function add data into the SQL table. In the future I plan to have user-determined input added into the SQL Table.

javascript
sql
node.js
sql-server
visual-studio-2019
asked on Stack Overflow Aug 26, 2019 by Phanotak • edited Aug 26, 2019 by Dale K

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0