on 2025 Jan 06 5:35 PM
Hi All,
const cds = require('@sap/cds');
const sql = require('mssql');
module.exports = cds.service.impl(async function () {
this.on('READ', 'YourEntity', async (req) => {
try {
// SQL Server Connection Config
const config = {
user: process.env.SQL_USER,
password: process.env.SQL_PASSWORD,
server: process.env.SQL_SERVER,
database: process.env.SQL_DATABASE,
options: {
encrypt: true, // Use encryption if necessary
trustServerCertificate: true // Change to false in production
}
};
// Connect to SQL Server
const pool = await sql.connect(config);
const result = await pool.request().query('SELECT * FROM YourTable');
return result.recordset;
} catch (err) {
console.error(err);
req.reject(500, 'Error connecting to SQL Server');
}
});
});
Regards,
Kowsalyaa Kumar
Request clarification before answering.
Hi @kowsalyaa ,
My requirement was same to connect with SQL Database and i'm able to connect to SQL DB as well.
Above code looks fine, just make sure libraries are updated in package.json.
Sharing mine.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Pmayank ,
I have the same issue trying to reach the server of the SQL Server configured in the cloud conector using the mssql library for nodejs.
Did you configure anything else apart of binding the connectivity servíce to the cap project?
Seems that the project is not able to resolve the virtual host and virtual port configured in cloud conector.
Thanks in advance!
A few things:
1. There should not be a problem connecting to MS SQL server if you have the right node package.
2. Your problem seems to be a problem that it cannot find or resolve the host name of your server.
3. There is at present no "standard SAP SQL Server adapter". For node, the only ones that are available (for Node) are HANA, Postgres and sqlite.
4. If you wanted to build it correctly, you would package your SQL adapter in a CAP plugin "shell" similar to the adapters you find in https://github.com/cap-js/cds-dbs
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 18 | |
| 6 | |
| 6 | |
| 6 | |
| 6 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.