Link to home
Start Free TrialLog in
Avatar of Chaste Swedge
Chaste SwedgeFlag for United States of America

asked on

How to use SQL Server for Configuration data in C# startup.cs

I'm having an issue trying to get configuration data from an Azure SQL Server database into the Startup.cs program in a C# MVC program. The backend is utilizing dapper for calls to SQL Server, and the front-end is Angular 11.

I can't seem to find the proper way to use dapper (obviously don't want to call internal controller designed for the front-end Angular app to populate config data in the backend). I could create a 'service', but then how and where would I reference it?

Could I utilize the same internal dapper code (interface not controller) to get the data I want, or would I have to create new code that performed the same tasks (running stored proc's in dapper) so that Configure or ConfigureServices would work correctly?

Thanks!
Avatar of Peter Chan
Peter Chan
Flag of Hong Kong image

Check the following. Ensure relevant linkage is able to pick up data.
https://dzone.com/articles/connecting-angular-to-an-sql-database

Avatar of Andrei Fomitchev
I don't see App.config in VS 2019 anymore. So, I created a regular XML File:
App.Config:
<Repository>
   <MachineName>DESKTOP</MachineName>
   <SQLServerName>SS17</SQLServerName>
</Repository>

// in C#
string sysDirectory = @"C:\FomAndDBATools";
string sysConfig = "App.Config";
string fullFileName = sysDirectory + '\\' + sysConfig;

string[] lines = File.ReadAllLines(fullFileName);
for(int k = 0; k < lines.Length; k++) { line += lines[k]; }
string repoStr = getStr(line, "Repository");
string machine = getStr(repoStr, "MachineName");
string sqlServer = getStr(repoStr, "SQLServerName");
string conStr = $"Data Source={machine}\\{sqlServer};Initial Catalog=master;
Integrated Security=true";
SqlConnection con = new SqlConnection(conStr);

        string getStr(string line, string tag)
        { // I am using a quite light parser for XML
            string s = "";
            string start = "<" + tag + ">";
            string finish = "</" + tag + ">";
            int iStart = line.IndexOf(start)+start.Length;
            int iFinish = line.IndexOf(finish);
            s = line.Substring(iStart, iFinish - iStart).Trim();
            return s;
        }


Avatar of Chaste Swedge

ASKER

dead question.
ASKER CERTIFIED SOLUTION
Avatar of Chaste Swedge
Chaste Swedge
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial