I have a ConfigureServices method in Startup.cs which configures my sql connection string.
public void ConfigureServices(IServiceCollection services)
{
String connpath;
try
{
StreamReader sr = new StreamReader("C:\\configfolder\\config.txt");
connpath = sr.ReadLine();
sr.Close();
Console.WriteLine(connpath);
var connection = @'"' + connpath + '"';
services.AddDbContext<DCCLog_Context>
(options => options.UseSqlServer(connection));
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("End Finally");
}
}
Currently I'm facing issues moving from hard-coded sql connection to a dynamic one, because the tutorial given by MSDN uses var
instead of SqlConnection
, as shown below.
var connection = @"Server=DESKTOP\SQLEXPRESS;Database=xLogDB;Trusted_Connection=True;MultipleActiveResultSets=True";
services.AddDbContext<xLog_Context>
(options => options.UseSqlServer(connection));
How do I make my sql connection a dynamic one, that reads from a text file?