Hello!!!
I made a connection to MSSQL by copying the module to the node_modules folder.
I tried to connect in two ways, but neither of them works. There is no connection.
Both versions of the code work locally.
I connected according to the method
Example code 1 variant:
const sql = require(‘mssql’);
const config = {
user: ‘test’,
password: ‘test’,
server: ‘test’,
database: ‘test’,
authentication: {
type: ‘default’
},
options: {
encrypt: true
}
}
console.log(“Starting…”);
connectAndQuery();
async function connectAndQuery() {
const searchEmail = “test@test.com”
try {
var poolConnection = await sql.connect(config);
console.log("Reading rows from the Table...");
var resultSet = await poolConnection.request().query(`SELECT Email, Id, Salt FROM Users WHERE Email LIKE '%${searchEmail}%'`);
const result = resultSet.recordset
console.log(result)
console.log(result.length)
poolConnection.close();
} catch (err) {
console.error(err.message);
}
}
Example code variant 2:
var Connection = require(‘tedious’).Connection;
var config = {
server: ‘test’,
authentication: {
type: ‘default’,
options: {
userName: ‘test’,
password: ‘test’
}
},
options: {
encrypt: true,
database: ‘test’
}
};
var connection = new Connection(config);
connection.on(‘connect’, function(err) {
console.log(“Connected”);
executeStatement();
});
connection.connect();
const searchEmail = "test@test.com"
var Request = require('tedious').Request;
function executeStatement() {
request = new Request(`SELECT Email, Id, Salt FROM Users WHERE Email LIKE '%${searchEmail}%'`, function(err) {
if (err) {
console.log(err);}
});
var result = "";
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
request.on('done', function(rowCount, more) {
console.log(rowCount + ' rows returned');
});
request.on("requestCompleted", function (rowCount, more) {
connection.close();
});
connection.execSql(request);
}
Thank you in advance.