How to connect Microsoft SQL Server with Node js Application #mssqlserver #nodejs #nodejstutorial



How to connect Microsoft SQL Server with Node js Application #mssqlserver #nodejs #nodejstutorial

How to connect Microsoft SQL Server with Node js Application #mssqlserver #nodejs #nodejstutorial

How to connect SQL Server with Node js application.

1. Create One folder with your application name

2. Go to file path and type cmd and hit enter then command prompt will be open with your application path

3. Now run this command- npm init –y (This will create a package.josn file)

4. Now type code . (Code space dot -This will open VS Code with this application)

5. Now open new terminal and run the below command
i. npm install (To install all package)

ii. npm install msnodesqlv8 –save (This will install MSSQL related package)

This will add the below line of code in this package.json file

“dependencies”: {
“msnodesqlv8”: “^3.0.1”
}
6. Now go to package.json file and paste the below code
“devDependencies”: {
“nodemon”: “^2.0.4”
},

And now go to “scripts”:{} section in this file (package.json)
And add this line of code- “start”: “nodemon api.js”

7. Now create file with the name dbConfig.js (name can be choosen by you)
And write the below code –

const connectionString=”server=DESKTOP-6AR2PM3;Database=myDB;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}”;
module.exports=connectionString;

8. Now create one file with the name dbOperations.js (name can be chosen by you)
And write the below code –

var config=require(‘./dbConfig’);
const sql=require(‘msnodesqlv8’);

async function getLoginDetails(){
try {
sql.query(config,”select * from tblUser”,(err,rows)=*{
console.log(rows);
})
} catch (error ) {
console.log(error);
}
}
module.exports={
getLoginDetails:getLoginDetails
}
//Just replace the * with angle bracket as Youtube doesn’t allow angel bracket in description.

9. Now Create one file with the name api.js (name can be chosen by you)
And write the below code –

const { reset } = require(‘nodemon’);
const dboperations = require(‘./dbOperations’);
var Db=require(‘./dbOperations’);

dboperations.getLoginDetails().then(result=*{
console.log(result);
})
//Just replace the * with angle bracket as Youtube doesn’t allow angel bracket in description.
10. Now run the project with the below command in terminal
npm start

11. Now you will be able to see the all list.

email for the source code- [email protected]
Also like and subscribe my channel and plz follow us on Instagram
https://Interviewclassroom.com
https://www.instagram.com/interview_crack/
https://www.youtube.com/@UC18-u9WGuTcshq4c8ONizgQ