As @Timon Yang-MSFT indicated you can still use an App.config
file but the favored way under .NET Core is to read connection strings and other settings in a file named appsettings.json
.
If you were using ASP.NET Core appsettings.json
is easy to implement for reading connection strings while those writing desktop applications require to write code.
I created a simple GitHub repository as a starter for getting a connection string from appsettings.json
where in this case there are three connection strings, one for development, one for test and one for production. If you have only one environment then as coded set the development connection.
Example appsettings.json
{
"ConnectionStrings": {
"DevelopmentConnection": "Data Source=.\\SQLEXPRESS;Initial Catalog=NorthWind2020;Integrated Security=True",
"ProductionConnection": "Data Source=ProductionServer;Initial Catalog=NorthWind2020;Integrated Security=True",
"TestConnection": "Data Source=TestServer;Initial Catalog=NorthWind2020;Integrated Security=True",
"Environment": 2
}
}
Example usage to read from a SQL-Server database
using System.Data.SqlClient;
using SqlServerConnectionLibrary;
namespace AppSettingsCoreUnitTestProject.Classes
{
public class SqlOperations
{
public static string ConnectionString = "";
public static CustomerRelation GetCustomers()
{
InitializeConnection();
CustomerRelation customer = new();
var selectStatement = "TODO";
using var cn = new SqlConnection() { ConnectionString = ConnectionString };
using var cmd = new SqlCommand() { Connection = cn, CommandText = selectStatement };
cn.Open();
var reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
customer.CustomerIdentifier = reader.GetInt32(0);
customer.CompanyName = reader.GetString(1);
customer.City = reader.GetString(2);
customer.PostalCode = reader.GetString(3);
customer.ContactId = reader.GetInt32(4);
customer.CountryIdentifier = reader.GetInt32(5);
customer.Country = reader.GetString(6);
customer.Phone = reader.GetString(7);
customer.PhoneTypeIdentifier = reader.GetInt32(8);
customer.ContactPhoneNumber = reader.GetString(9);
customer.ModifiedDate = reader.GetDateTime(10);
customer.FirstName = reader.GetString(11);
customer.LastName = reader.GetString(12);
}
return customer;
}
private static void InitializeConnection()
{
if (!string.IsNullOrWhiteSpace(ConnectionString)) return;
Helper.Initializer();
ConnectionString = Helper.ConnectionString;
}
}
}
We can have other properties stored in appsettings.json e.g.
{
"GeneralSettings": {
"LogExceptions": true,
"DatabaseSettings": {
"DatabaseServer": ".\\SQLEXPRESS",
"Catalog": "School",
"IntegratedSecurity": true,
"UsingLogging": true
},
"EmailSettings": {
"Host": "smtp.gmail.com",
"Port": 587,
"EnableSsl": true,
"DefaultCredentials": false,
"PickupDirectoryLocation": "MailDrop"
}
}
}
Not included but wanted to show other configuration possibilities
And last note, with not much effort we can programmatically change settings in the appsettings.json file. With that, if you can a setting while the app is running the change will be recognized.
GitHub solution