Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views2 pages

Proiect Ghe

This document connects an Express application to a SQL Server database. It queries two tables, renders the data to an EJS template, and includes a POST route to insert new data into one of the tables.

Uploaded by

Hahue Mihai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Proiect Ghe

This document connects an Express application to a SQL Server database. It queries two tables, renders the data to an EJS template, and includes a POST route to insert new data into one of the tables.

Uploaded by

Hahue Mihai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const express = require("express");

const sql = require("mssql/msnodesqlv8");

const app = express();


const port = 3000;

var config = {
server: "DESKTOP-K3NQCCN\\SQLEXPRESS",
database: "Evidenta dotarilor din facultate",
driver: "msnodesqlv8",
options: {
trustedConnection: true,
},
};

// Serve static files from the "views" directory


app.use(express.static("views"));

app.set("view engine", "ejs");

app.get("/", (req, res) => {


sql.connect(config, function (error) {
if (error) {
console.log(error);
res.send("Error connecting to the database");
return;
}

var request = new sql.Request();


request.query("select * from Departamente", function (error, records) {
if (error) {
console.log(error);
res.send("Error querying the database");
sql.close();
return;
}

var firstSet = records.recordset;

var anotherRequest = new sql.Request();


anotherRequest.query("select * from Studenti", function (error,
anotherRecords) {
if (error) {
console.log(error);
res.send("Error querying the database");
sql.close();
return;
}

var secondSet = anotherRecords.recordset;

res.render("index", { firstSet, secondSet });

sql.close();
});
});
});
});
app.use(express.urlencoded({ extended: true })); // middleware pentru a parsa
corpul cererii

app.post("/insert", (req, res) => {


const { nume, prenume, departament } = req.body;

sql.connect(config, function (error) {


if (error) {
console.log(error);
res.send("Error connecting to the database");
return;
}

var request = new sql.Request();

// Exemplu de interogare INSERT. Modifică-o în funcție de structura tabelului


tău.
request.query(`INSERT INTO Studenti (Nume, Prenume, Departament) VALUES ('$
{nume}', '${prenume}', '${departament}')`, function (error) {
if (error) {
console.log(error);
res.send("Error inserting data into the database");
sql.close();
return;
}

res.redirect(`http://localhost:${port}/`); // Redirecționează la pagina


principală după inserare
sql.close();
});
});
});

app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});

You might also like