Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Global Connection Strings

Posted by Praveen Kumar on March 8, 2008

Here i am going to describe how you can create a global connection for all pages.
as i have already describe in my web.config and global.asax(please refer it to know more) category
This is my web.config file where i may store number of connection.
Here i create two connetion with ODBC driver and SQL Server driver

 web.config

<configuration>
  <connectionStrings>
    <add name=”mySqlConn” connectionString=”Driver={MySQL ODBC 5.1 Driver};Server=192.168.2.140;Database=payroll_master;Uid=praveen;”/>
    <add name=”sqlConnString” connectionString=”Driver={SQL Server};Server=127.0.0.1;database=mydatabase;Uid=sa;”/>
  </connectionStrings>
  <appSettings>
    <!–<add key=”myConn” value=”provider=MySQL ODBC 5.1 Driver;server=192.168.2.140;database=payroll_master;User Id=praveen;”/>–>
  </appSettings>

and this is my seperate class file named ConnectionClass which use connectionstrings defined in web.config file

ConnectionClass.cs 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

public class ConnectionClass : System.Web.UI.Page
{
    OdbcConnection conn;
     public ConnectionClass()
 {
 }
   public OdbcConnection Connect_thru_config()
    {
        string connString;
//If you want to create MySQL connection then you need to pass only mySqlConn
 //connString = System.Configuration.ConfigurationManager.ConnectionStrings[“mySqlConn”].ConnectionString;
        connString = System.Configuration.ConfigurationManager.ConnectionStrings[“sqlConnString”].ConnectionString;//for Sql
        OdbcConnection conwithfig = new OdbcConnection(connString);
        return conwithfig;
     }
}

and this is the ConnectionDemo.aspx.cs page where i am calling Connect_thru_config()

ConnectionDemo.aspx.cs 

try
        {
            ConnectionClass conobj = new ConnectionClass();
            configcn = conobj.Connect_thru_config();
            configcn.Open();
            DataSet ds = new DataSet();
            string sql = “select *from FK_TBLUSER”;
            OdbcDataAdapter da = new OdbcDataAdapter(sql, configcn);
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        } catch (Exception e1)     {       Page.Response.Write(“error” + e1);       }
        finally        {            configcn.Close();        }
    }

Conclusion–

This article describes that you may have number of connection in config file and call these connection strings in a seperate class file in a method, and you may use this class anywhere in your project. you do not need to write connection string in each page.
I hope this articles may help you. If you want to give any suggestion please you can leave your comment to improve this article.
your comments and suggestion will be most appreciable.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: