Click here to Skip to main content
12,623,027 members (29,515 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET C#4.0 .NET4
I have created form with textboxes and different button(add,update.delete) with two different databases(one sql and other ms access).i had taken two connection strings

<connectionStrings>
        <!--<add name="test1" connectionString="Data Source=asd;Initial Catalog=shivani;User ID=sa;Password=*****" providerName="System.Data.SqlClient"/>-->
        <add name="test" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ShivaniSharma\Documents\transaction.accdb"/>
    </connectionStrings>
  <connectionStrings>
    <add name="test1" connectionString="Data Source=dasd;Initial Catalog=shivani;User ID=sa;Password=*****" providerName="System.Data.SqlClient"/>
    <!--<add name="test" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ShivaniSharma\Documents\transaction.accdb"/>-->
  </connectionStrings>

public partial class _Default : System.Web.UI.Page
{
   sql objsql = new sql(System.Configuration.ConfigurationManager.ConnectionStrings["test1"].ConnectionString);
   access objaccess = new access(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ConnectionString);
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {//on clicking button update respective database
        objsql.update(Convert.ToDouble(txtAmt.Text), txtFrom.Text);
       objaccess.update(Convert.ToDouble(txtAmt.Text), txtFrom.Text);
    }
}

here i want to know one i want to use particular connectionstring based on upon choice in web.config. I would like to have option in web.config.i want to use only one connectoinstring at a time.if i use option for "test" it should fire only sqlconnection and if i use option for "test1" it should fire only oledb connection.

How can I achieve this?

regards
Posted 8-Feb-13 1:28am
Updated 8-Feb-13 3:05am
v3
Comments
Tadit Dash 8-Feb-13 7:02am
   
What is the problem with current code?
shivani 2013 8-Feb-13 7:07am
   
there is no problem in code.....i want to have option in web.config.i want to use only one connectoinstring at a time.if i use option of "test" it should fire only sqlconnection and if i use option of "test1" it should fire only oledb connection how can i achieve this
regards
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

In your code when you are connecting to your data access layer or to your database you can use
System.Configuration.ConfigurationManager.ConnectionStrings
to get access to the different ConnectionStrings.

See this article for more.[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

modifying web.config
 <!--i took here key values on changing value for desired database- -->
<appSettings>
   <add key="Connection" value="2"/>
 
 </appSettings>
 <connectionStrings>
 
 <!--  this is for SQL database; right now i am selecting for access database so i commented it if i want to use sql i"ll change key value to 1 and uncomment it and comment access database
           <!--<add name="test" connectionString="Data Source="";Initial Catalog=shivani;User ID=sa;Password=""; providerName="System.Data.SqlClient" />-->
 

   <add name="test" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ShivaniSharma\Documents\transaction.accdb"  />
 
 </connectionStrings>
 
<pre lang="c#"><!--   on form    -->
 Business_layer.ClsMultiDB objMDB= new Business_layer.ClsMultiDB(2,ConfigurationManager.ConnectionStrings["test"].ConnectionString) ;
 
objMDB.update(Convert.ToDouble(txtAmt.Text), txtFrom.Text);


namespace Business_layer
{
   public class ClsMultiDB
    {
        datalayer.DataLayer Objdl;
 
        public ClsMultiDB(Int16 DbType, String ConnectionString)
        {
            Objdl = new datalayer.DataLayer(DbType, ConnectionString);
        }
 
        public void update(double bal, string acc)
        {
            int up = Objdl.DMLQuery("Update tblAccount set dbalance =  dBalance - " + bal + " where AccNumber = '"
 
                + acc + "'");
 

 
        }
namespace datalayer
{
    public class DataLayer
    {
        public Int16 DLDBType = 0;
        public String DLConnectionString = "";
 
        public DataLayer(Int16 DbType,String ConnectionString)
        {
            if (DbType == 0)
            {
               // throw InvalidProgramException;
            }
            else
            {
                DLDBType = DbType;
                DLConnectionString = ConnectionString;
            }
        }
 
        public int DMLQuery(String SqlCommandText)
        {
            int DMLValue = 0;
            if (DLDBType == 1)
            {
                SQL ObjSQL = new SQL(DLConnectionString);
                DMLValue= ObjSQL.DMLQuery(SqlCommandText);
            }
 
            if (DLDBType == 2)
            {
              msaccess Objms = new msaccess(DLConnectionString);
                DMLValue = Objms.DMLQuery(SqlCommandText);
            }
 
            return DMLValue;
        }
 

    }

and create classes for SQL and msaccess class in datalater namespace to define connection........
hope it helps and if any suggestion always welcome
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100