简体   繁体   English

我们如何在 Web.Config 中有两个连接字符串并在后面的代码中在它们之间切换?

[英]How Can We Have two Connection Strings In Web.Config And Switch Betweeen Them In Code Behind?

When I add two connection strings in the web.config, an error appears that tells me I can't add two connection strings in the web.config.当我在 web.config 中添加两个连接字符串时,会出现一个错误,告诉我无法在 web.config 中添加两个连接字符串。

I want the upper job because I have 2 databases and I want transfer data from another to the other one.我想要上层工作,因为我有 2 个数据库,我想将数据从另一个数据库传输到另一个数据库。

Would you please show me a way for doing that?你能告诉我这样做的方法吗?

When you add a connection string, you name it.添加连接字符串时,您可以为其命名。

You can access each such connection string and assign it to a different variable, passing that connection string to your data access layer.您可以访问每个这样的连接字符串并将其分配给不同的变量,将该连接字符串传递给您的数据访问层。

In the config file:在配置文件中:

<connectionStrings>

  <add name="Sales" 
       providerName="System.Data.SqlClient"
       connectionString= "server=myserver;database=Products;uid=<user name>;pwd=<secure password>" />

  <add name="NorthWind" 
       providerName="System.Data.SqlClient" 
       connectionString="server=.;database=NorthWind;Integrated Security=SSPI" />

</connectionStrings>

In your code:在您的代码中:

 var conn1 = ConfigurationManager.ConnectionStrings["Sales"].ConnectionString;
 var conn2 = ConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;

Simply put those strings in your web.config:只需将这些字符串放在您的 web.config 中:

<connectionStrings>
    <add name="CS1"
         connectionString="SOME CONNECTION STRING"
         providerName="System.Data.SqlClient" />
    <add name="CS2"
         connectionString="SOME OTHER STRING"
         providerName="System.Data.SqlClient" />
</connectionStrings>

And then pick the one you wish in your code:然后在代码中选择你想要的:

string cs = ConfigurationManager.ConnectionStrings["CS2"].ConnectionString;

We can declare multiple connection strings under Web.Config or App.Config:我们可以在 Web.Config 或 App.Config 下声明多个连接字符串:

<connectionStrings>
    <add name="SourceDB" connectionString="..." />
    <add name="DestinationDB" connectionString="..." />
</connectionStrings>

In DAL you can access connection strings according to your requirements:在 DAL 中,您可以根据您的要求访问连接字符串:

string SounceConnection = ConfigurationManager.ConnectionStrings["SourceDB"].ConnectionString;
string DestinationConnection = ConfigurationManager.ConnectionStrings["DestinationDB"].ConnectionString;

You can add all the connectionstrings that you want to web.config.您可以将所有您想要的连接字符串添加到 web.config。 But they must have different names.但它们必须有不同的名称。

Strange, because you can specify multiple connection strings.奇怪,因为可以指定多个连接字符串。 They simply must have different names.它们只是必须具有不同的名称。

Here are the steps:以下是步骤:

 public class KisanDbContext : DbContext
    {
        public KisanDbContext() : base(nameOrConnectionString: "DbContext") { }
        public KisanDbContext(string conn) : base(nameOrConnectionString: conn) { }
    }

Access methods:访问方法:

 private readonly KisanDbContext db;
 private readonly KisanDbContext db_old;
   

Inside your constructor:在您的构造函数中:

public className()
{
    db = new KisanDbContext();
    db_old = new KisanDbContext("DbContextBackup");
}

This one is for your default connection.这是您的默认连接。

public KisanDbContext() : base(nameOrConnectionString: "DbContext") { }

And if you want your multiple database connection please used this convention, where you can pass multiple database connection strings using same dbcontext.如果您想要多个数据库连接,请使用此约定,您可以在其中使用相同的 dbcontext 传递多个数据库连接字符串。

public KisanDbContext(string conn) : base(nameOrConnectionString: conn) { }

Remember to have your connection string with different name as:请记住让您的连接字符串具有不同的名称:

<connectionStrings>
     <add name="DbContext" connectionString="Server=A;Database=ABC;User 
     Id=ccd;Password=***"  providerName="System.Data.SqlClient" />
    <add name="DbContextBackup" connectionString="B;Database=DEF;User Id=axx;Password=***;   
    providerName="System.Data.SqlClient" />  
</connectionStrings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Web.Config中的2个连接字符串之间切换(为DBML激活一个) - How can I switch between 2 Connection Strings in my Web.Config (Activate one for DBML) 我在web.config中需要两个连接字符串吗? - Do I need two connection strings in web.config? 如何更改Web.config中数据库的连接字符串,这些字符串具有基于LOGIN数据库的Login_User表的数据库名称 - How can I change connection strings for database in my web.config that have database name based on Login_User table from LOGIN database web.config 到一个 SQL DB 的两个连接字符串在调试中工作,但在发布时不起作用 - Two connection strings in web.config to one SQL DB work in debug but not when published 我可以通过Web.Config连接字符串优化数据库连接吗? - Can I optimize my database connections via Web.Config connection strings? 在Code-Behind中从Web.Config中检索HTML - Retrieve HTML from Web.Config in Code-Behind 建议使用web.config转换保护连接字符串的方法 - Suggested methods to secure connection strings with web.config transformations Web.config c#中连接字符串的加密 - Encryption of connection strings in Web.config c# 从后面的代码编辑 web.config 中的 httpErrors 部分 - Edit httpErrors section in web.config from code behind Visual Studio Online Build - web.config连接字符串configSource - Visual Studio Online Build - web.config connection strings configSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM