Chaste Swedge
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!
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!
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;
}
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;
}
ASKER
dead question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
https://dzone.com/articles/connecting-angular-to-an-sql-database