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 {  /* */  }
	}

         /* ... */

}

How can we centralize all the connection strings in the iBATIS configuration?

On 11/8/06, Gilles Bayon <ibatis.net@gmail.com> wrote:
> You can place them in the web.config/app.config
>
> <appSettings>
> <add key="database" value="first connection string" />
> <add key="databaseTest" value="2nd connection string" />
> </appSettings>
>
> and initialize a session variable "ConnectionString" that is init with the
> choice of the user and used in the per user datasource

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