Dashboard > iBATIS DataMapper > Home > Frequently Asked Questions > How do I set the connection string per user in Web context
How do I set the connection string per user in Web context
Added by Gilles Bayon, last edited by Gilles Bayon on Nov 04, 2006  (view change)
Labels: 
(None)


After having build the SqlMapper instance with the DomSqlMapBuilder
Replace the SqlMapper DataSource with

SqlMapper.DataSource = new PerUserDataSource( SqlMapper.DataSource );

where PerUserDataSource class is something like this

PerUserDataSource.cs
public class PerUserDataSource : DataSource
    {
    IDataSource _innerDataSource = null;
    
    public PerUserDataSource( IDataSource innerDataSource)
    {
    _innerDataSource = innerDataSource;
    }
    
    public override string ConnectionString
    {
    get
    {
    // build the connectionstring base on the user session setting
    			HttpContext currentContext = HttpContext.Current;
    string connectionString = "data source=10.1.2.3;database=DBTest;user id=#userid#;password=#password#;";
    return connectionString.Replace(currentContext.Session["userid"]).Replace(currentContext.Session["password"]);
    }
    set { /* */ }
    }
    
    public override IDbProvider DbProvider
    {
    get { return _innerDataSource.DbProvider ; }
    set {  /* */  }
    }
    
    /* ... */
    
    }

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators