How do you change SubSonic 3's connection string on the fly?

可紊 提交于 2019-12-10 11:40:29

问题


I want to wrap some queries in a SharedDbConnectionScope and execute them under a different connection string. How do I add a provider/connection string dynamically in order to do this?

Thanks


回答1:


Both the ActiveRecord\Context.tt and the LinqTemplates\Context.tt that you would use to generate your classes contain constructors:

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

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

So you can pass your connection string to one of these constructors, like:

// 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");


来源:https://stackoverflow.com/questions/9881626/how-do-you-change-subsonic-3s-connection-string-on-the-fly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!