How do I connect from a Web App to a Database using ODBC driver?

How do I connect from a Web App to a Database using ODBC driver? I can successfully connect to the Database in my Desktop App. But if I upload my app to the Web App Server the connection cannot be established.

 Accepted Answer

Since the Web App Server likely runs on a different machine it does not have the same setup. The Datasource might be missing on the machine where the Web App Server is running and the Web App Worker User might not be authorized to access the database.
In order to use an ODBC data source from a Web App, you have 2 options:
Option 1: Create an ODBC data source using ODBC Administrator window on the target machine. The ODBC data source name should match to one used in the code.
OR
Option 2: Use a DSN-less connection string to connect to your database using ODBC.
We introduced an ODBC function for this in R2021a:https://www.mathworks.com/help/database/ug/odbc.html
This function takes a DSN-less connection string. This eliminates the need to create an ODBC Data source. Instead, you need to provide all the parameters as a connection string. 
We recommend using the second option, as this feature was specifically introduced to enable ODBC workflows in deployed environments, eliminating pre-config steps on target machines. 
As mentioned before you also need to make sure that the user of the Web App Server (which is MwWebAppWorkerR20XXn) has the necessary permissions to access the Database Server. An example of how to do this in Microsoft SQL Server Management Studio is shown below.
You need to provide the following Server Roles in the Properties of the MwWebAppWorkerR20XXn user:

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!