Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Connection String from Web.config in asp.net

I want to know the ways to get connection string from web.config file in asp.net.

I just only know the below way .

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration;      namespace Sherserve.DataAccessLayer     {         public class DBGateway         {             public static string conString;              public DBGateway()             {                 conString = ConfigurationManager.ConnectionStrings["test"].ToString();             }         }     } 
like image 770
Ammar Asjad Avatar asked Sep 19 '12 07:09

Ammar Asjad


People also ask

How do you read connection string from configuration file into code behind?

To read the connection string into your code, use the ConfigurationManager class. string connStr = ConfigurationManager. ConnectionStrings["myConnectionString"].

Where are connectionStrings in web config?

Connection strings can be added any where in configuration in such a way it should be a child of configuration. Its recommended that it should be placed after all tags so it remains visible if you need to change it in future.

What is connection string providerName?

The providerName attribute is used to set the name of the .NET Framework data provider that the DataSource control uses to connect to an underlying data source. If no provider is set, the default is the ADO.NET provider for Microsoft SQL Server.


2 Answers

Using the ConfigurationManager.ConnectionStrings is about the only proper way, to use it properly with sanity check you can have such code:

public DBGateway() {     ConnectionStringSettings mySetting = ConfigurationManager.ConnectionStrings["test"];     if (mySetting == null || string.IsNullOrEmpty(mySetting.ConnectionString))         throw new Exception("Fatal error: missing connecting string in web.config file");     conString = mySetting.ConnectionString; } 

This will throw useful error in case the connection string is missing, instead of cryptic "null object" error.

Worth to mention that the ConnectionStringSettings class is overriding the ToString() method:

public override string ToString() {     return this.ConnectionString; } 

So it means that using ConfigurationManager.ConnectionStrings["test"].ToString() is the same like ConfigurationManager.ConnectionStrings["test"].ConnectionString however you still better perform sanity check and personally it looks cleaner to use the actual property and not depend on the class to give it.

like image 200
Shadow Wizard Hates Omicron Avatar answered Sep 21 '22 04:09

Shadow Wizard Hates Omicron


Here is the whole solution:-

string constring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString; SqlConnection con = new SqlConnection(constring); DataSet ds = new DataSet(); try  {    SqlDataAdapter dataAdapter = new SqlDataAdapter(query, con);    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);    con.Open();    dataAdapter.Fill(ds, "table");    return ds;  }  catch (Exception ex)   {   }     finally     {         if (con.State == System.Data.ConnectionState.Open)             con.Close();     } 

This is how you can fetch records from database into datatable.

Hope this is what you were looking for.

like image 34
RL89 Avatar answered Sep 21 '22 04:09

RL89