I have a big problem: I want to to connect to a MS SQL Server but whatever I am doing it doesn't work... I don't know where my mistake lies..
const parse = require("csv-parse/lib/sync");
const fs = require("fs");
const path = require("path");
var mssql = require('mssql/msnodesqlv8');
var iconv = require('iconv-lite');
var http = require('http');
// Connection to SSMS Database
var dbConfig = {
host: 'localhost',
user: 'user',
server: 'server',
database: 'database',
options: {
trustedConnection: true,
useUTC: true
}
};
var connection = new mssql.Connection(dbConfig, function(err) {
var request = new mssql.Request(connection);
...
request.query(`INSERT INTO ${richtigername} (${namen}) VALUES (${values})`, function (err, recordset) {
if (err) {
console.log(err);
res.send(recordset)
}
mssql.close();
});
Now there is the error message:
TypeError: request is not a function
It is so difficult to get data into a SQL Server database.. the tool works fine with MySQL but SQL Server is horrible..
I have two more queries before this with also: request.query(...)
and if (err) { console.log(err); res.send(recordset)
if (err) { console.log(err); res.send(recordset)
... MySQL is on the other hand so easy... only
connection.query(...)
and before this
// Verbindung zur MySQL Datenbank
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'database'
});
and
// Verbindung zur Datenbank starten
connection.connect((error) => {
if (error) {
console.error(error);
} else {
}
})
}
It might be a type in your question, but it looks like you're missing a closing braces.
You have an open braces for this function
var connection = new mssql.Connection(dbConfig, function(err) {
But I don't see closing braces at the end.
First, assign a connection pool then use that to run your SQL. This example uses a stored procedure, but it is easily modified to run a query.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
var sql = require("mssql");
// config for your database
var config = {
user: 'user',
password: 'yourpassword',
server: 'yourserver',
database: 'yourdb'
};
new sql.ConnectionPool(config).connect().then(pool => {
return pool.request()
// for select query, uncomment .query and comment .input and .execute
//.query("select * from table1 where id = 2")
// for stored proc, comment .query and uncomment .input and .execute
// .input is only needed if proc has params
.input('p_id', sql.Int, 5)
.execute('getData')
}).then(result => {
let rows = result.recordset
res.setHeader('Access-Control-Allow-Origin', '*')
res.status(200).json(rows);
sql.close();
}).catch(err => {
console.log(err);
res.status(500).send({ message: "${err}"})
sql.close();
});
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});
why it is so difficult to run an easy query to SQL Sever.. There are so many different solutions.. I had try this:
//Tabellen aus Datenbank anzeigen und speichern
exports.tableNames = function (req, res) {
sql.connect(config).then(function () {
var sqlrequest = new sql.Request();
sqlrequest.query('Select TABLE_NAME from TreasuryBI.INFORMATION_SCHEMA.tables').then(function (recordset) {
console.dir(recordset);
sql.close();
return res.status(200).send(recordset);
}).catch(function (err) {
console.log(err);
});
});
};
the problem is that I am unfortunately not a computer scientist or something.. Therefore, my knowledge is limited.. but I hope you could help me..
Best regards Frederic
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.