[英]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.