@@ -484,6 +484,96 @@ console.log("File read successfully");
484484## Day 9.0 - Events
485485###No Update...
486486
487+ ## Day 9.1 : Nodejs connection to SQL
488+
489+ In this session i am going to demonstrate how we can connect to database.
490+
491+ 1 . I am taking the example of how we can connect to SQL database.
492+ 2 . First you need to take the refrence of MSDatabase.
493+ 3 . For more details you will get on below link. <br />
494+ https://www.npmjs.com/package/node-mssql - it is old ways<br />
495+ 4 . Or by using mssql, For more details you can see below link.<br />
496+ https://www.npmjs.com/package/mssql <br />
497+ Install mssql package like below
498+ ``` ruby
499+ npm install mssql
500+ ```
501+
502+ 5 . You need to enable sql Network services from services.msc. <br />
503+
504+ 6 . Enable tcp/ip protocol as mentioned in below link of your system. <br />
505+ https://stackoverflow.com/questions/25577248/node-js-mssql-tedius-connectionerror-failed-to-connect-to-localhost1433-conn
506+
507+ 7 . Restart your system to see the effect of changes. <br />
508+ 8 . Import MSSQL to application. <br />
509+ ``` ruby
510+ var sql = require (' mssql' );
511+ ```
512+ 9 . Create the configuration for DB connection.
513+ ``` ruby
514+ const config = {
515+ user: ' sa' ,
516+ password: ' password1' ,
517+ server: ' REETPC' , // You can use ' localhost\\ instance' to connect to named instance
518+ database: ' test12' ,
519+ }
520+ ```
521+
522+ 10 . Inserting records to the database
523+ ``` ruby
524+ sql.connect(config).then(pool => {
525+
526+ return pool.request()
527+ .input(' CustName' , sql.VarChar (50 ), " satya" )
528+ .input(' Phonenumber' , sql.VarChar (10 ), " 45677889" )
529+ .input(' Age' , sql.Int , 23 )
530+ // You can either insert records by hard code query
531+ // .query(" INSERT INTO [test12].[dbo].[Customer] ([CustName] ,[Phonenumber] ,[Age]) VALUES( @CustName, @Phonenumber, @Age)" )
532+
533+ // OR you can write a store procedure and call that
534+ .execute(' InsertCustomer' ,)
535+ // .output(' output_parameter' , sql.VarChar (50 ))
536+ }).then(result => {
537+ console.dir(result)
538+ }).catch (err => {
539+ console.log(err);
540+ })
541+
542+ ```
543+
544+ 11 . Here is the way you can use to get the records using hard coded query
545+ ``` ruby
546+ sql.connect(config).then(() => {
547+ return sql.query` select * from [dbo].[Customer]`
548+ }).then(result => {
549+ console.log(result)
550+ }).catch (err => {
551+ console.log(err);
552+ })
553+ ```
554+
555+ 12 . Fetch the result using store procedure.
556+ ``` ruby
557+ sql.connect(config).then(pool => {
558+ return pool.request().execute(" GetAllCustomer" )
559+ }).then(result => {
560+ console.log(result)
561+ }).catch (err => {
562+ console.log(err);
563+ })
564+ ```
565+
566+ 13 . Fetch the selected result using store procedure.
567+ ``` ruby
568+ sql.connect(config).then(pool => {
569+ return pool.request().input(' Customerid' , sql.Int , 2 ).execute(" GetCustomerbyId" )
570+ }).then(result => {
571+ console.log(result)
572+ }).catch (err => {
573+ console.log(err);
574+ })
575+ ```
576+
487577## Day 10 - ExpressJs
488578
489579ExpressJs use to build the Node Application, it make faster the devlopment and provide inbuild code for development.<br /><br />
0 commit comments