如何在web.config文件中提供两个不同的数据库连接. [英] how to give two different database connections in web.config file.

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

问题描述

请帮助.....
我想在一个asp.net项目中使用两个不同的数据库.假设我有2个数据库A和B,我想从数据库A调用一些存储过程,并从数据库B调用一些存储过程.然后,
如何在connectionstring中提供2个数据库连接?

在此先感谢.....

pls help.....
i want to use two different database in my one asp.net project. suppose i have 2 database A & B, i want to call some stored procedures from database A and some stored procedures call from database B. Then,
how to give 2 database connections in connectionstring?

Thanks in advance.....

推荐答案

在web.config中,您可以有两个不同的(按名称)连接字符串,如下所示:

In web.config, you can have two distinct (by name) connectionstring as:

<configuration>
  <connectionstrings>
    <add name="MyConnection1" connectionstring="Data Source=server1;Initial Catalog=db1;User ID=id1;Password=pass1;" providername="System.Data.EntityClient" />
    <add name="MyConnection2" connectionstring="Data Source=server2;Initial Catalog=db2;User ID=id2;Password=pass2;" providername="System.Data.EntityClient" />
  </connectionstrings>
  <system.web></system.web></configuration>



在后面的代码中,您可以在两个不同的支架中获取这两个连接,如下所示:



In the code behind, you can fetch these two connection in two different holder as:

private static string connectionString1 = ConfigurationManager.ConnectionStrings["MyConnection1"].ToString();
private static string connectionString2 = ConfigurationManager.ConnectionStrings["MyConnection2"].ToString();



希望,它可以澄清您的问题.



Hope, it clarify your question.


只需在<connectionstrings></connectionstrings>块内使用2个连接字符串.

调用存储过程时,请使用连接数据库所需的数据库.
Just use 2 connection string inside <connectionstrings></connectionstrings> block.

Use the one you need to connect the db when you call stored procedure.


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

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