如何在web.config中为不同的数据库使用多个连接字符串? [英] how to use multiple connectionstring in web.config for different databases?

查看:66
本文介绍了如何在web.config中为不同的数据库使用多个连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了带有文本框的表单和不同的按钮(add,update.delete)和两个不同的数据库(一个sql和其他ms访问)。我已经使用了两个连接字符串



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);
    }
}





这里我想知道一个我想根据选择使用特定的连接字符串web.config中。我想在web.config.i中有选项想要一次只使用一个connectoinstring。如果我使用test选项它应该只触发sqlconnection,如果我使用test1选项它应该只触发oledb连接。



我怎样才能实现这个目标?



问候



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

推荐答案

在您的代码中,当您连接到数据访问层或数据库时,您可以使用
In your code when you are connecting to your data access layer or to your database you can use
System.Configuration.ConfigurationManager.ConnectionStrings

来获取访问不同的ConnectionStrings。



请参阅这篇文章了解更多。 [ ^ ]

to get access to the different ConnectionStrings.

See this article for more.[^]


修改web.config

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;
        }


    }





并为SQL和msaccess类创建类datalater命名空间定义连接........

希望它有所帮助,如果有任何建议总是欢迎



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


这篇关于如何在web.config中为不同的数据库使用多个连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆