尝试获取类型数据库的实例时发生激活错误,仅在动态创建连接字符串时发生错误 [英] Activation error occured while trying to get instance of type database, error occurring only when connection string is created dynamically

查看:109
本文介绍了尝试获取类型数据库的实例时发生激活错误,仅在动态创建连接字符串时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码(.NET Framework 4.5.2,Enterprise Library 5.0.505.0),我需要连接到SQL Server数据库。但是,数据库名称可能会根据用户的要求不断变化。所以,我有以下代码将动态连接字符串写入app.config文件。



I have a piece of code (.NET Framework 4.5.2, Enterprise Library 5.0.505.0) where I need to connect to a SQL Server database. However, the DB name may keep changing depending on user's requirement. So, I have the following piece of code to write the connection string dynamically to the app.config file.

public void CreateNewConnectionStringInConfig(string initialCatalog)
{
      SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
      builder.DataSource = ConfigurationManager.AppSettings["Data Source"];
      builder.PersistSecurityInfo = true;
      builder.InitialCatalog = initialCatalog; //This is the DB name
      builder.UserID = ConfigurationManager.AppSettings["User ID"];
      builder.Password = ConfigurationManager.AppSettings["Password"];

      // Get the application configuration file.
      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

      // Create a connection string element.
      ConnectionStringSettings csSettings = new ConnectionStringSettings("UserSelectedConnectionString", builder.ConnectionString, "System.Data.SqlClient");

      // Get the connection strings section.
      ConnectionStringsSection csSection = config.ConnectionStrings;

      // Add the new element.
      csSection.ConnectionStrings.Add(csSettings);

      // Save the configuration file.
      config.Save(ConfigurationSaveMode.Modified);

     // Refresh the section so the new configuration can be re-read
      ConfigurationManager.RefreshSection("connectionStrings");

}





我检查了连接字符串,并在vshost.exe.Config文件中正确创建了连接字符串在调试时。但是,当我尝试创建数据库对象时,我收到一个错误。用于创建数据库对象的代码如下所示。





I checked and the connection string is getting created fine in the vshost.exe.Config file while debugging. However, when I am trying to create the Database object, I am getting an error. The code used to create the DB object is as shown below.

public class MyDac
{
    private readonly Database _db;

    public MyDac()
    {
        DatabaseProviderFactory factory = new DatabaseProviderFactory();
        _db = factory.Create("UserSelectedConnectionString");
    }
}





尝试创建_db对象时出现以下错误。





I am getting the following error while trying to create the _db object.

Activation error occured while trying to get instance of type Database, key "UserSelectedConnectionString"<br />
<br />
Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type Database, key "UserSelectedConnectionString" --->     Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "UserSelectedConnectionString".<br />
Exception occurred while: while resolving.<br />
Exception is: InvalidOperationException - The type Database does not have an accessible constructor.





任何帮助都将非常感谢。谢谢!



我尝试过:



1)升级企业库版本到6.0.0.0解决了这个问题,但这不是我的选择。我必须将它保留到版本5.0.505.0。



2)当我从前面对App.config文件中的连接字符串进行硬编码时(而不是编写它)在运行时),该应用程序工作正常。但是,我不能在现实生活中这样做,因为数据库名称将继续改变。



Any help will be much appreciated. Thanks!

What I have tried:

1) Upgrading the Enterprise Library version to 6.0.0.0 resolves the issue but that is not an option for me. I have to keep it to version 5.0.505.0.

2) When I hard code the connection string in the App.config file from before hand (rather than writing it during run time), the app works fine. However, I can't do that in real life because the database name will keep on changing.

推荐答案

我改变了我的代码,如下所示,并开始工作。但是,我没有任何解释:



I changed my code as shown below and that started working. However, I do not have any explanation for it:

public class MyDac
{
    private readonly Database _db;
 
    public MyDac()
    {
        _db = new SqlDatabase("UserSelectedConnectionString");
    }
}


这篇关于尝试获取类型数据库的实例时发生激活错误,仅在动态创建连接字符串时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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