你如何动态改变SubSonic 3的连接字符串?

时间:2012-03-27 00:06:13

标签: c# subsonic subsonic3

我想在SharedDbConnectionScope中包装一些查询,并在不同的连接字符串下执行它们。如何动态添加提供者/连接字符串才能执行此操作?

由于

1 个答案:

答案 0 :(得分:2)

用于生成类的ActiveRecord\Context.ttLinqTemplates\Context.tt都包含构造函数:

    public <#=DatabaseName#>DB(string connectionStringName)
    {
        DataProvider = ProviderFactory.GetProvider(connectionStringName);
        Init();
    }

    public <#=DatabaseName#>DB(string connectionString, string providerName)
    {
        DataProvider = ProviderFactory.GetProvider(connectionString,providerName);
        Init();
    }

因此,您可以将连接字符串传递给其中一个构造函数,例如:

// point to a certain connection string in the app.config
var db = new MySample("SomeConnectionStringName");

// Use a specific connection string, not the app.config
var db = new MySampleDB(@"server=.\SQL2008;database=Sample;integrated security=true;", "System.Data.SqlClient");