Hello,
I have connected my database to Visual studio code, but Can somebody give an idea how to code in JavaScript to call a mssql database to extract Name and last name if account number exists?
I mean I know in VB you do something like:(This is just an example) But I do not how to in JavaScript.
con.Open()
cmd = New SqlCommand("Select * from dbo.ei_employee where code = '" & user_name.Text & "'and password = '" & password.Text & "' ", con)
sdr = cmd.ExecuteReader()
'view = "select * from dbo.ei_employee where code = '" & user_name.Text & "'"
'Dim ds As DataSet = GetData(view)
'If (ds.Tables.Count > 0) Then
' eid = sdr.GetValue(2)
If (sdr.Read()) Then
Session("user_name") = enam as string
End If
Thank you,


Jayraj ChhayaRank #304 · 8.2K reputationPosted Oct 10, 2024, 1:58 AM EDT
To extract data from an MSSQL database using JavaScript, you can utilize the
mssqlpackage, which allows you to connect and query SQL Server databases. Below is a simple example demonstrating how to retrieve the first name and last name based on an account number.First, ensure you have the
mssqlpackage installed:Then, you can use the following code snippet:
const sql = require('mssql'); async function getUserDetails(accountNumber) { try { const config = { user: 'your_username', password: 'your_password', server: 'your_server', database: 'your_database', options: { encrypt: true // Use this if you're on Azure } }; // Connect to the database await sql.connect(config); // Query the database const result = await sql.query`SELECT firstName, lastName FROM dbo.ei_employee WHERE accountNumber = ${accountNumber}`; if (result.recordset.length > 0) { const { firstName, lastName } = result.recordset[0]; console.log(`Name: ${firstName} ${lastName}`); } else { console.log('No user found with the provided account number.'); } } catch (err) { console.error('SQL error', err); } finally { await sql.close(); } } // Example usage getUserDetails('12345');Replace
your_username,your_password,your_server, andyour_databasewith your actual database credentials. The functiongetUserDetailstakes an account number as an argument and retrieves the corresponding first and last names from theei_employeetable.Jignesh KumarRank #91 · 22.9K reputationPosted Oct 10, 2024, 5:56 AM EDT
const mysql = require('mysql'); // Create a connection to the database const connection = mysql.createConnection({ host: 'localhost', // Your database host user: 'your_username', // Your database username password: 'your_password', // Your database password database: 'your_database' // Your database name }); // Connect to the database connection.connect((err) => { if (err) { console.error('Error connecting to the database:', err); return; } console.log('Connected to the database.'); }); // Function to get names const getNames = () => { return new Promise((resolve, reject) => { const query = 'SELECT first_name, last_name FROM ei_employee where code = '+ YourUserName +' '; connection.query(query, (error, results) => { if (error) { reject(error); } else { resolve(results); } }); }); }; // Use the function getNames() .then((names) => { names.forEach((user) => { console.log(`First Name: ${user.first_name}, Last Name: ${user.last_name}`); }); }) .catch((error) => { console.error('Error fetching names:', error); }) .finally(() => { connection.end(); // Close the connection });