Connection to Oracle Database Via an API Settings File

Viewed 32

I'm trying to learn API's as we have a business case for it. and for the most part I can read and Serialize/Deserialize the data from our Oracle database - HOWEVER, in order to connect to the database im hardcoding the connectionString variable at the top of the page e.g

con = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=my.oracle.host)(PORT=9999)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=myOracleService)));User Id=XXXXXX;Password=XXXXXX; ");

Ive created the OracleSettingsDB.cs file as below

public class OracleDbSettings{

        public string Host{get;set;}

        public string Port{get;set;}

        public string User{get;set;}

        public string Password{get;set;}

        public string Service{get;set;}

        public string ConnectionString{

            get
            {
                return $"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={Host})(PORT={Port})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={Service})));User Id={User};Password={Password};";

            }
        }
    }
}

and following on from that added the values to the appsettings.json

"AllowedHosts": "*",
  "OracleDbSettings": {
      "Host": "my.oracle.host",
      "Port":"9999",
      "User": "XXXXX",
      "Service":"myOracleService",
      "Password": "XXXXXX"  //Yes I know I shouldnt store this here
  }```

I added the connectionString into the Startup.cs

``` var connectionString = Configuration.GetSection(nameof(OracleDbSettings)).Get<OracleDbSettings>();

But what I dont now get is how i reference the connectionString within the Repository file so that I can call

using (OracleConnection con = new OracleConnection(ConnectionString)

Hopefully, someone will be able to point me in the right direction as all the videos Ive watched on Youtube or pages ive found just reference the hardcoded data source - which we dont want to do.

Thanks and Hopefully theres enough information there =]

1 Answers

I'm afraid you need to bind the values in appsettings.json file to your OracleDbSettings model, and I find similar document about how to connect to oracle which matches your using (OracleConnection con = new OracleConnection(ConnectionString), so I provide the tutorial here to solve your requirement. And this is my test details:

I newly created an asp.net core 5 mvc project and create a model like yours:

namespace WebMvcApp.Models
{
    public class OracleDbSettings
    {
        public const string keyword = "OracleDbSettings";
        public string Host { get; set; }
        public string Port { get; set; }
        public string User { get; set; }
        public string Password { get; set; }
        public string Service { get; set; }
        public string ConnectionString
        {
            get
            {
                return $"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={Host})(PORT={Port})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={Service})));User Id={User};Password={Password};";
            }
        }
    }
}

Modify the startup.cs file-> Configure method, and adding these line-code:

var dbSettings = new OracleDbSettings();
Configuration.GetSection(OracleDbSettings.keyword).Bind(dbSettings);
var connect_string = dbSettings.ConnectionString;

And my appsettings.json looks like this:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "OracleDbSettings": {
    "Host": "my.oracle.host",
    "Port": "9999",
    "User": "XXXXX",
    "Service": "myOracleService",
    "Password": "XXXXXX"
  }
}

enter image description here

Related