locked
DB2 connection string in MVC 6 RRS feed

  • Question

  • User1704461893 posted

    I'm coming back to .net after a several years.  I've got a basic application running locally in VS 2015 but I need to connect to a DB2 as400 backend. I selected an mvc 5 web app but it seems to have a some mvc 6 features. Unless I'm mistaken the connection string needs to go in the appsettings.json. My searches have failed to find me a successful method to connect. Can someone verify
       -how to connect
       -where to put the connection string
       -the connection string format

    "DB2Connection": {
    "ConnectionString": "Server=myAddress:myPortNumber;Database=myDataBase;UID=myUsername;PWD=myPassword;"
    }
    any url walkthrough would be helpful
    Thanks in advance

    Tuesday, August 9, 2016 5:36 PM

Answers

All replies

  • User-821857111 posted

    You should try starting an MVC 5 application again. MVC Core (the new name for MVC 6) is a lot different to previous versions, and is not really recommended unless you plan to develop an app that can be deployed in a non-windows environment.

    Otherwise, you can look at the ASP.NET Core docs: https://docs.asp.net/en/latest/fundamentals/configuration.html#getting-and-setting-configuration-settings

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 9, 2016 7:03 PM
  • User-491950272 posted

    The connection strings are stored in appsettings.json file in the form objects and sub-objects. But the required connection string in appsettings.json as:

    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=myAddress:myPortNumber;Database=myDataBase;UID=myUsername;PWD=myPassword;"
      }
    }

    Configure it in Startup.cs class constructor as:

    public Startup(IHostingEnvironment env)
            {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(env.ContentRootPath)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
    
                CurrentEnvironment = env;
    
                builder.AddEnvironmentVariables();
                Configuration = builder.Build();
    
            }

    And then read the connection string form the configuration property as: Configruation["ConnectionStrings:DefaultConnection"].

    I've also written a one-one blog for using EFCore in ASP.NET Core here. ASP.NET Core documentation can also be helpful.

    Tuesday, August 9, 2016 9:05 PM
  • User-2057865890 posted

    Hi jgracia134,

    ASP.NET stores application configuration settings in Web.config. For example, developers use the <appSettings> section to store custom application settings, the <connectionStrings> section to store database connection strings. ASP.NET Core uses AppSettings.json to store such pieces of information. Something that looks like this:

    {
       "AppSettings": {
          "Title": "My ASP.NET Core Application"
       },
     
       "Data": {
          "DefaultConnection": {
             "ConnectionString": "data source=.;
                initial catalog=Northwind;integrated security=true"
          }
       }
    }

    reference: Getting Configuration Settings In ASP.NET Core MVC

    Best Regards,

    Chris

    Wednesday, August 10, 2016 3:07 PM
  • User1704461893 posted

    Thank you very much for the reply Mike.  I took your advice and restarted the project in MVC 5. I will make my priority connecting the the back end from there and will educate myself in the new MVC in the mean time.

    Wednesday, August 10, 2016 8:31 PM
  • User1704461893 posted

    Janshair and Chris.  I will look thoroughly into your answers when not developing the restarted MVC 5 project.  After several years away from .NET my coding is slow and labored.   Thank you both very much.  

    Wednesday, August 10, 2016 8:37 PM