We help IT Professionals succeed at work.
Get Started

How can i avoid repeating a connection string already defined in the appconfig file

164 Views
Last Modified: 2020-01-31
I have a connection string in the appconfig file of my c# application  (see below) that writes to a postgresql database.

AppConfig

[]<connectionStrings>
    <add name="Tournaments" connectionString="Host = 138.197.165.89; Database = tournaments; Port = 5432; Username = postgres" />
  </connectionStrings>[/code]

In the method below that goes into my Database  I have had to repeat the string again  (see below)

[] public class PgrConnector : IDataConnection
    {
        private const string db = "tournaments";
        private const string pgrstring = "Host = 138.197.165.89; Database = tournaments; Port = 5432; Username = postgres";
     
        public void ClosePayrollTestRun(MonthPayModel model)
        {
            using (NpgsqlConnection conn = new NpgsqlConnection(pgrstring))
            {

                using (var command = new NpgsqlCommand("public.sppayrollproccesor_closepayrolltestrun", conn))
                {
                    conn.Open();
                    command.CommandType = CommandType.StoredProcedure;

                    //foreach (DataRow row in dataSource.)
                    //{
                    command.Parameters.Add(new NpgsqlParameter("companycodex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticcompany });
                    command.Parameters.Add(new NpgsqlParameter("mnthyearx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticmmyyyy });
                    //  command.Parameters.Add(new NpgsqlParameter("staffnox", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticstaffno });
                    // command.Parameters.Add(new NpgsqlParameter("entcodex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticentcode });
                    string output = "";
                    output = Convert.ToString(command.ExecuteScalar());
                    LoginDetails.staticpayrollproccesorstatus = output;
                    if (output == "-1")
                    {
                        StandardMessages.PayrollScriptError();
                    }
                    return;
                    //}
                }
            }
        }[/code]

I want to avoid the repetition by changing

 using (NpgsqlConnection conn = new NpgsqlConnection(pgrstring))

so that it does not use pgrstring  but the connectionstring coming from my Appconfig file

What would the correct syntax  for the using statement

Thanks

Olukay
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 14 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE