Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
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

XML
<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>


C#
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
Updated 8-Feb-13 2:05am
v3
Comments
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

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

See this article for more.[^]
 
Share this answer
 
modifying web.config
XML
 <!--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);



C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900