I am trying to connect to a MS SQL Server DB using node.js. I installed the msnodesql module to use for this task. I am able to connect to a DB with the following code:
var sql = require('msnodesql');
var util = require('util');
//
var connStr = "Driver={SQL Server Native Client 11.0};Server=myySqlDb,1433;Database=DB;UID=Henry;PWD=cat;";
var query = "SELECT * FROM GAData WHERE TestID = 17";
sql.open(connStr, function(err,conn){
if(err){
return console.error("Could not connect to sql: ", err);
}
conn.query(query,function(err,results){
if (err){
return console.error("Error running query: ", err);
}
console.log(results);
console.log(results.length);
for (var i = 0; i <= results.length; i++){
util.inspect(results[i]);
}
});
});
My goal however is to connect to the DB from various events, such as button submits, from a HTML page. From the button click I want to call a node.js function to query the DB for a particular attribute, such as the following:
From the HTML:
<br /><button type="submit" id="continue" onClick="verifyEmail($('#email').val())">Continue</button>
From the script file:
function verifyEmail(email){
var mQuery = "'EXEC WebUserRecovery '" + email + "'";
sql.open(conn_str, function (err, conn) {
if (err) {
console.log("Error opening the connection!\r\n" + err);
return;
}
connection.query(mQuery,function(err,results){
if (err){
return console.error("Error running query: ", err);
}
alert(results);
});
});
}
The code when put inside the function does not work, a DB connection is unsuccessful. Can anyone advise how to fix this issue? There is little good documentation on msnodesql out there...
verifyEmail()executes at client or at node.js server?$.ajax()