I need help on how to make an insert in sql server from node js [closed]
My question is how can I send the driver of my web api to make an insert to my database from node js ?, the web api is in visual studio and the node js in visual studio code. An acquaintance says that with a request I can do it, but I am very confused on how it should be done. If anyone can help I'd appreciate it.
javascript html node.js asp.net-web-api bootstrap-4
closed as too broad by SherylHohman, CozyAzure, Mark, lucascaro, Nguyễn Thanh Tú Nov 20 '18 at 7:40
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
My question is how can I send the driver of my web api to make an insert to my database from node js ?, the web api is in visual studio and the node js in visual studio code. An acquaintance says that with a request I can do it, but I am very confused on how it should be done. If anyone can help I'd appreciate it.
javascript html node.js asp.net-web-api bootstrap-4
closed as too broad by SherylHohman, CozyAzure, Mark, lucascaro, Nguyễn Thanh Tú Nov 20 '18 at 7:40
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
My question is how can I send the driver of my web api to make an insert to my database from node js ?, the web api is in visual studio and the node js in visual studio code. An acquaintance says that with a request I can do it, but I am very confused on how it should be done. If anyone can help I'd appreciate it.
javascript html node.js asp.net-web-api bootstrap-4
My question is how can I send the driver of my web api to make an insert to my database from node js ?, the web api is in visual studio and the node js in visual studio code. An acquaintance says that with a request I can do it, but I am very confused on how it should be done. If anyone can help I'd appreciate it.
javascript html node.js asp.net-web-api bootstrap-4
javascript html node.js asp.net-web-api bootstrap-4
asked Nov 20 '18 at 2:47
Leon9091Leon9091
1
1
closed as too broad by SherylHohman, CozyAzure, Mark, lucascaro, Nguyễn Thanh Tú Nov 20 '18 at 7:40
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by SherylHohman, CozyAzure, Mark, lucascaro, Nguyễn Thanh Tú Nov 20 '18 at 7:40
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
https://www.w3schools.com/nodejs/nodejs_mysql.asp:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
Then simply run the file from the terminal. I know on lunix it's $ node file.js
but I'm using sql server, not mysql, and the connection and procedures, I have them in the web visual studio api
– Leon9091
Nov 20 '18 at 2:53
It should be the same thing, I think.
– BGM52
Nov 20 '18 at 2:54
the professor said that the information should be sent in this way: html> Javascript> Node.js> Web Api> SQL
– Leon9091
Nov 20 '18 at 2:57
What OS are you using?
– BGM52
Nov 20 '18 at 2:57
I'm using windows
– Leon9091
Nov 20 '18 at 3:01
|
show 2 more comments
if you are using sql server, you can use tedious:
const Request = require('tedious').Request;
const Connection = require('tedious').Connection;
const DB = {
async connect() {
const config = {
userName: your username,
password: your password,
server: your host,
options: {
port: port,
database: database,
rowCollectionOnRequestCompletion: true,
rowCollectionOnDone: true,
},
};
return new Promise((resolve, reject) => {
const connection = new Connection(config);
connection.on('connect', (err) => {
if (err) {
console.log(`connect error: ${err.message}`);
reject(err);
} else {
resolve(connection);
}
});
});
},
async request(sql) {
const connection = await DB.connect();
return new Promise((resolve, reject) => {
connection.execSql(new Request(sql, (error, rowCount, rows) => {
connection.close();
if (error) {
reject(error);
} else {
resolve({
rowCount,
rows
});
}
}));
});
},
};
module.exports = DB;
If I use sql server, but I do not do anything from the node js, I have a connection between the node js and the web api, it is assumed that in the node js I send everything from the web api, not from sql server directly
– Leon9091
Nov 20 '18 at 3:32
I must send call the procedures and the database from node, but my database is in the web api of visual studio
– Leon9091
Nov 20 '18 at 3:36
I have an example, but it will not let me upload the code
– Leon9091
Nov 20 '18 at 3:41
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
https://www.w3schools.com/nodejs/nodejs_mysql.asp:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
Then simply run the file from the terminal. I know on lunix it's $ node file.js
but I'm using sql server, not mysql, and the connection and procedures, I have them in the web visual studio api
– Leon9091
Nov 20 '18 at 2:53
It should be the same thing, I think.
– BGM52
Nov 20 '18 at 2:54
the professor said that the information should be sent in this way: html> Javascript> Node.js> Web Api> SQL
– Leon9091
Nov 20 '18 at 2:57
What OS are you using?
– BGM52
Nov 20 '18 at 2:57
I'm using windows
– Leon9091
Nov 20 '18 at 3:01
|
show 2 more comments
https://www.w3schools.com/nodejs/nodejs_mysql.asp:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
Then simply run the file from the terminal. I know on lunix it's $ node file.js
but I'm using sql server, not mysql, and the connection and procedures, I have them in the web visual studio api
– Leon9091
Nov 20 '18 at 2:53
It should be the same thing, I think.
– BGM52
Nov 20 '18 at 2:54
the professor said that the information should be sent in this way: html> Javascript> Node.js> Web Api> SQL
– Leon9091
Nov 20 '18 at 2:57
What OS are you using?
– BGM52
Nov 20 '18 at 2:57
I'm using windows
– Leon9091
Nov 20 '18 at 3:01
|
show 2 more comments
https://www.w3schools.com/nodejs/nodejs_mysql.asp:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
Then simply run the file from the terminal. I know on lunix it's $ node file.js
https://www.w3schools.com/nodejs/nodejs_mysql.asp:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
Then simply run the file from the terminal. I know on lunix it's $ node file.js
answered Nov 20 '18 at 2:52
BGM52BGM52
378
378
but I'm using sql server, not mysql, and the connection and procedures, I have them in the web visual studio api
– Leon9091
Nov 20 '18 at 2:53
It should be the same thing, I think.
– BGM52
Nov 20 '18 at 2:54
the professor said that the information should be sent in this way: html> Javascript> Node.js> Web Api> SQL
– Leon9091
Nov 20 '18 at 2:57
What OS are you using?
– BGM52
Nov 20 '18 at 2:57
I'm using windows
– Leon9091
Nov 20 '18 at 3:01
|
show 2 more comments
but I'm using sql server, not mysql, and the connection and procedures, I have them in the web visual studio api
– Leon9091
Nov 20 '18 at 2:53
It should be the same thing, I think.
– BGM52
Nov 20 '18 at 2:54
the professor said that the information should be sent in this way: html> Javascript> Node.js> Web Api> SQL
– Leon9091
Nov 20 '18 at 2:57
What OS are you using?
– BGM52
Nov 20 '18 at 2:57
I'm using windows
– Leon9091
Nov 20 '18 at 3:01
but I'm using sql server, not mysql, and the connection and procedures, I have them in the web visual studio api
– Leon9091
Nov 20 '18 at 2:53
but I'm using sql server, not mysql, and the connection and procedures, I have them in the web visual studio api
– Leon9091
Nov 20 '18 at 2:53
It should be the same thing, I think.
– BGM52
Nov 20 '18 at 2:54
It should be the same thing, I think.
– BGM52
Nov 20 '18 at 2:54
the professor said that the information should be sent in this way: html> Javascript> Node.js> Web Api> SQL
– Leon9091
Nov 20 '18 at 2:57
the professor said that the information should be sent in this way: html> Javascript> Node.js> Web Api> SQL
– Leon9091
Nov 20 '18 at 2:57
What OS are you using?
– BGM52
Nov 20 '18 at 2:57
What OS are you using?
– BGM52
Nov 20 '18 at 2:57
I'm using windows
– Leon9091
Nov 20 '18 at 3:01
I'm using windows
– Leon9091
Nov 20 '18 at 3:01
|
show 2 more comments
if you are using sql server, you can use tedious:
const Request = require('tedious').Request;
const Connection = require('tedious').Connection;
const DB = {
async connect() {
const config = {
userName: your username,
password: your password,
server: your host,
options: {
port: port,
database: database,
rowCollectionOnRequestCompletion: true,
rowCollectionOnDone: true,
},
};
return new Promise((resolve, reject) => {
const connection = new Connection(config);
connection.on('connect', (err) => {
if (err) {
console.log(`connect error: ${err.message}`);
reject(err);
} else {
resolve(connection);
}
});
});
},
async request(sql) {
const connection = await DB.connect();
return new Promise((resolve, reject) => {
connection.execSql(new Request(sql, (error, rowCount, rows) => {
connection.close();
if (error) {
reject(error);
} else {
resolve({
rowCount,
rows
});
}
}));
});
},
};
module.exports = DB;
If I use sql server, but I do not do anything from the node js, I have a connection between the node js and the web api, it is assumed that in the node js I send everything from the web api, not from sql server directly
– Leon9091
Nov 20 '18 at 3:32
I must send call the procedures and the database from node, but my database is in the web api of visual studio
– Leon9091
Nov 20 '18 at 3:36
I have an example, but it will not let me upload the code
– Leon9091
Nov 20 '18 at 3:41
add a comment |
if you are using sql server, you can use tedious:
const Request = require('tedious').Request;
const Connection = require('tedious').Connection;
const DB = {
async connect() {
const config = {
userName: your username,
password: your password,
server: your host,
options: {
port: port,
database: database,
rowCollectionOnRequestCompletion: true,
rowCollectionOnDone: true,
},
};
return new Promise((resolve, reject) => {
const connection = new Connection(config);
connection.on('connect', (err) => {
if (err) {
console.log(`connect error: ${err.message}`);
reject(err);
} else {
resolve(connection);
}
});
});
},
async request(sql) {
const connection = await DB.connect();
return new Promise((resolve, reject) => {
connection.execSql(new Request(sql, (error, rowCount, rows) => {
connection.close();
if (error) {
reject(error);
} else {
resolve({
rowCount,
rows
});
}
}));
});
},
};
module.exports = DB;
If I use sql server, but I do not do anything from the node js, I have a connection between the node js and the web api, it is assumed that in the node js I send everything from the web api, not from sql server directly
– Leon9091
Nov 20 '18 at 3:32
I must send call the procedures and the database from node, but my database is in the web api of visual studio
– Leon9091
Nov 20 '18 at 3:36
I have an example, but it will not let me upload the code
– Leon9091
Nov 20 '18 at 3:41
add a comment |
if you are using sql server, you can use tedious:
const Request = require('tedious').Request;
const Connection = require('tedious').Connection;
const DB = {
async connect() {
const config = {
userName: your username,
password: your password,
server: your host,
options: {
port: port,
database: database,
rowCollectionOnRequestCompletion: true,
rowCollectionOnDone: true,
},
};
return new Promise((resolve, reject) => {
const connection = new Connection(config);
connection.on('connect', (err) => {
if (err) {
console.log(`connect error: ${err.message}`);
reject(err);
} else {
resolve(connection);
}
});
});
},
async request(sql) {
const connection = await DB.connect();
return new Promise((resolve, reject) => {
connection.execSql(new Request(sql, (error, rowCount, rows) => {
connection.close();
if (error) {
reject(error);
} else {
resolve({
rowCount,
rows
});
}
}));
});
},
};
module.exports = DB;
if you are using sql server, you can use tedious:
const Request = require('tedious').Request;
const Connection = require('tedious').Connection;
const DB = {
async connect() {
const config = {
userName: your username,
password: your password,
server: your host,
options: {
port: port,
database: database,
rowCollectionOnRequestCompletion: true,
rowCollectionOnDone: true,
},
};
return new Promise((resolve, reject) => {
const connection = new Connection(config);
connection.on('connect', (err) => {
if (err) {
console.log(`connect error: ${err.message}`);
reject(err);
} else {
resolve(connection);
}
});
});
},
async request(sql) {
const connection = await DB.connect();
return new Promise((resolve, reject) => {
connection.execSql(new Request(sql, (error, rowCount, rows) => {
connection.close();
if (error) {
reject(error);
} else {
resolve({
rowCount,
rows
});
}
}));
});
},
};
module.exports = DB;
answered Nov 20 '18 at 3:24
pengpeng
234
234
If I use sql server, but I do not do anything from the node js, I have a connection between the node js and the web api, it is assumed that in the node js I send everything from the web api, not from sql server directly
– Leon9091
Nov 20 '18 at 3:32
I must send call the procedures and the database from node, but my database is in the web api of visual studio
– Leon9091
Nov 20 '18 at 3:36
I have an example, but it will not let me upload the code
– Leon9091
Nov 20 '18 at 3:41
add a comment |
If I use sql server, but I do not do anything from the node js, I have a connection between the node js and the web api, it is assumed that in the node js I send everything from the web api, not from sql server directly
– Leon9091
Nov 20 '18 at 3:32
I must send call the procedures and the database from node, but my database is in the web api of visual studio
– Leon9091
Nov 20 '18 at 3:36
I have an example, but it will not let me upload the code
– Leon9091
Nov 20 '18 at 3:41
If I use sql server, but I do not do anything from the node js, I have a connection between the node js and the web api, it is assumed that in the node js I send everything from the web api, not from sql server directly
– Leon9091
Nov 20 '18 at 3:32
If I use sql server, but I do not do anything from the node js, I have a connection between the node js and the web api, it is assumed that in the node js I send everything from the web api, not from sql server directly
– Leon9091
Nov 20 '18 at 3:32
I must send call the procedures and the database from node, but my database is in the web api of visual studio
– Leon9091
Nov 20 '18 at 3:36
I must send call the procedures and the database from node, but my database is in the web api of visual studio
– Leon9091
Nov 20 '18 at 3:36
I have an example, but it will not let me upload the code
– Leon9091
Nov 20 '18 at 3:41
I have an example, but it will not let me upload the code
– Leon9091
Nov 20 '18 at 3:41
add a comment |