C#: ASP.NET: get connection string from Web.config file

Trabla: C#: ASP.NET: get connection string from Web.config file


Solving:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


using Npgsql;

// Add this directive
using System.Configuration;

namespace MyApp.Models{

      public class MyReport{

           /// <summary> 
           /// 
           /// </summary> 
           /// <param name="database"> Database connection string name in Web.config</param> 
           /// <param name="params "></param> 
           /// <returns></returns>
            public static ExecuteReport1( string database, string[] params ){
                  
             string connStr = ConfigurationManager.ConnectionStrings[ database ].ConnectionString;
             
             // Example , Connect to PostgreSQL database via Npgsql
            NpgsqlConnection conn = new NpgsqlConnection(  connStr );
            
            // DO STAFF
             ....

            }

      }

}

No comments:

Post a Comment