如何更改Enterprise Library 5.0中的数据库

发布于 2024-12-06 16:29:00 字数 289 浏览 0 评论 0原文

我创建了一个数据库对象,

sqlDB = EnterpriseLibraryContainer.Current
    .GetInstance<Database>("ProdConn");

但后来在代码中,我想更改数据库名称。 在以前的企业版本中,我们经常

conn.ChangeDatabase("ABCD");

更改数据库,但是这里如何更改数据库呢?

请指教。

谢谢, 穆吉布。

I created a db object as

sqlDB = EnterpriseLibraryContainer.Current
    .GetInstance<Database>("ProdConn");

But later in code, i want to change the Database name.
In previous enterprise version, we use

conn.ChangeDatabase("ABCD");

to change the database but how we can do it here?

Please advice.

thanks,
Mujeeb.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌化 2024-12-13 16:29:00

我不认为 ChangeDatabase 是一个企业库方法(我在 4.1 版本中也找不到它)。我认为这只是 IDbConnection< 上的 ADO 方法< /代码>

我可以想到 3 种方法来完成您想要的操作:

  1. 在 Enterprise Library 配置中创建一个新的数据库条目并使用该值
  2. 使用 ADO.NET 更改连接并执行数据访问
  3. 以编程方式创建一个新的 Enterprise Library Database< /code> 使用不同数据库值的对象

1. 在配置中创建一个新的数据库条目

就个人而言,我认为这是最干净的选项。将数据库添加为配置中的新条目并将其视为单独的数据库。但是,如果您需要支持动态数据库,因为数据库名称在设计时未知或从不同的系统检索,那么这将不起作用。

2. 使用 ADO.NET

您可以检索连接并仅使用 ADO.NET(我认为这可能是您已经在做的事情?):

// Get Original EL DB
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MYDB");
object result = db.ExecuteScalar(CommandType.Text, 
    "select top 1 name from sysobjects");

Console.WriteLine(result);

// Change DB with ADO.NET
using (IDbConnection conn = db.CreateConnection())
{
    conn.Open();
    conn.ChangeDatabase("AnotherDB");

    using (IDbCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "select top 1 RoleName from Roles";
        cmd.CommandType = CommandType.Text;

        result = cmd.ExecuteScalar();
    }
}

Console.WriteLine(result);

不过,将 EL 代码与 ADO.NET 代码混合感觉有点错误。

3. 创建新的 Enterprise Library 数据库对象

除了使用 ADO.NET,您还可以使用 Enterprise Library Database 类。您无法修改 ConnectionString(它是 readonly),但您可以使用新的连接字符串创建新的 Database 对象。

// Get Original EL DB
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MYDB");
object result = db.ExecuteScalar(System.Data.CommandType.Text, 
    "select top 1 name from sysobjects");

Console.WriteLine(result);

// Change Database
DbConnectionStringBuilder builder = new DbConnectionStringBuilder()
{
    ConnectionString = db.ConnectionString
};

builder["database"] = "AnotherDB";

// Create new EL DB using new connection string
db = new GenericDatabase(builder.ConnectionString, db.DbProviderFactory);
result = db.ExecuteScalar(CommandType.Text, 
    "select top 1 RoleName from Roles");

Console.WriteLine(result);

我认为这看起来比选项 2 更好。我们可以通过将更改数据库逻辑添加到辅助方法或扩展方法(如下所示)来使其更简洁:

public static class DatabaseExtensions
{
    public static Database ChangeDatabase(this Database db, string databaseName)
    {
        // Change Database
        DbConnectionStringBuilder builder = new DbConnectionStringBuilder()
        {
            ConnectionString = db.ConnectionString
        };

        builder["database"] = databaseName;

        // Create new EL DB using new connection string
        return new GenericDatabase(builder.ConnectionString, 
            db.DbProviderFactory);
    }
}

...

// Get Original EL DB
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MYDB");
object result = db.ExecuteScalar(System.Data.CommandType.Text, 
    "select top 1 name from sysobjects");

Console.WriteLine(result);

db = db.ChangeDatabase("AnotherDB");

result = db.ExecuteScalar(CommandType.Text,
    "select top 1 RoleName from Roles");

Console.WriteLine(result);

I don't think ChangeDatabase is an Enterprise Library method (I couldn't find it in version 4.1 either). I think it's just an ADO method on IDbConnection.

There are 3 ways I can think of to do what you want:

  1. Create a new Database entry in Enterprise Library configuration and use that value
  2. Use ADO.NET to change the connection and perform data access
  3. Programmatically create a new Enterprise Library Database object using a different database value

1. Create a new Database Entry in Config

Personally, I think this is the cleanest option. Add the database as a new entry in configuration and treat it as a separate database. However, if you need to support dynamic databases because the database names are not known at design time or are retrieved from a different system then this won't work.

2. Use ADO.NET

You can retrieve a connection and just use ADO.NET (I think this may be what you were already doing?):

// Get Original EL DB
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MYDB");
object result = db.ExecuteScalar(CommandType.Text, 
    "select top 1 name from sysobjects");

Console.WriteLine(result);

// Change DB with ADO.NET
using (IDbConnection conn = db.CreateConnection())
{
    conn.Open();
    conn.ChangeDatabase("AnotherDB");

    using (IDbCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "select top 1 RoleName from Roles";
        cmd.CommandType = CommandType.Text;

        result = cmd.ExecuteScalar();
    }
}

Console.WriteLine(result);

Mixing the EL code with the ADO.NET code feels a bit wrong, though.

3. Create a new Enterprise Library Database Object

Instead of using ADO.NET you can use the Enterprise Library Database class. You can't modify the ConnectionString (it's readonly) but you can create a new Database object with a new connection string.

// Get Original EL DB
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MYDB");
object result = db.ExecuteScalar(System.Data.CommandType.Text, 
    "select top 1 name from sysobjects");

Console.WriteLine(result);

// Change Database
DbConnectionStringBuilder builder = new DbConnectionStringBuilder()
{
    ConnectionString = db.ConnectionString
};

builder["database"] = "AnotherDB";

// Create new EL DB using new connection string
db = new GenericDatabase(builder.ConnectionString, db.DbProviderFactory);
result = db.ExecuteScalar(CommandType.Text, 
    "select top 1 RoleName from Roles");

Console.WriteLine(result);

I think this looks better than option 2. We can make it a bit cleaner by adding the change database logic to a helper method or, as in the following, an extension method:

public static class DatabaseExtensions
{
    public static Database ChangeDatabase(this Database db, string databaseName)
    {
        // Change Database
        DbConnectionStringBuilder builder = new DbConnectionStringBuilder()
        {
            ConnectionString = db.ConnectionString
        };

        builder["database"] = databaseName;

        // Create new EL DB using new connection string
        return new GenericDatabase(builder.ConnectionString, 
            db.DbProviderFactory);
    }
}

...

// Get Original EL DB
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MYDB");
object result = db.ExecuteScalar(System.Data.CommandType.Text, 
    "select top 1 name from sysobjects");

Console.WriteLine(result);

db = db.ChangeDatabase("AnotherDB");

result = db.ExecuteScalar(CommandType.Text,
    "select top 1 RoleName from Roles");

Console.WriteLine(result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文