0

I am banging my head on the wall trying to figure out how to set up my connection string for my WPF .net core 3.1 app, Everything I see says to use app.config then when I look up app.config I see use appsettings.json, neither file is natively built into the project.

I feel like I am missing a baked-in way to add and access my connection string, please advise.

5
  • Username checks out Commented Sep 5, 2020 at 14:52
  • Can you share your starup.cs file? Commented Sep 5, 2020 at 14:57
  • .net core WPF app does not have a startup.cs @Red it isn't baked into the project. I am looking for a baked-in way to set up my connection string. Commented Sep 5, 2020 at 15:19
  • c-sharpcorner.com/article/… Commented Sep 5, 2020 at 15:28
  • Does this answer your question? Cannot add appsettings.json inside WPF project .net core 3.0 Commented Sep 5, 2020 at 16:20

1 Answer 1

0

Add app.config file and then get connectionstring with:

string GetAppSettingVal(string key)
        {
            Configuration config = null;
            string exeConfigPath = GetType().Assembly.Location;
            try
            {
                config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
            }
            catch (Exception ex)
            {
                return "";
            }

            var connectionString = config.AppSettings.CurrentConfiguration.ConnectionStrings.ConnectionStrings[key].ConnectionString; 

            if (!string.IsNullOrEmpty(connectionString))            
                return connectionString;
            
            return string.Empty;
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.