using clientcontext to get connectionstring

using clientcontext to get connectionstring

Old forum URL: forums.lhotka.net/forums/t/3690.aspx


Ton Smeets posted on Thursday, October 11, 2007

Hello,

I'm using multiple databases (test, demo, production1, production2) in my project. So far I can add and delete databases at runtime. The db-name and connectiostrings are saved to a secure database.

Now, when a user logs in, he has to choose a database to work with. To work this way I want to use clientcontext[database], which is set at login. At the server-side I want to create a object DataBaseConnection that has one factorymethod GetDbConnection(string DatabaseName). This method returns the connectionstring that belongs to the databasename.

Now, I looked all over the forum to find a sample of how I can put the clientcontext in my business-objects. The only way I can think of is the following:

private void DataPortal_Fetch(Criteria criteria)

{

using (SqlConnection cn = new SqlConnection(DataBaseConnection.GetDbConnection((string)Csla.ApplicationContext.ClientContext["database"]))

{

cn.Open();

using (SqlCommand cm = cn.CreateCommand())

{

cm.CommandType = CommandType.StoredProcedure;

Is this the right way to use clientcontext? Does this way of programming have side-effects when running local?

Thanx

JoeFallon1 replied on Thursday, October 11, 2007

Looks correct to me.

CleintContext travels from client to server (but not back).

So you should be OK in both Local and remoting situations.

Joe

 

Ton Smeets replied on Thursday, October 11, 2007

I tried a different code. Now I changed the sqlconnection back as it is used in project tracker. Imagine, ptoject tracker uses a demo, test and a production database. while loggin in I add clientcontext["dbName"] = "test".

Now i changed the database class as follows:

public static string PTrackerConnection

{

get

{

return ConfigurationManager.ConnectionStrings[Csla.ApplicationContext.ClientContext["dbName"].ToString()].ConnectionString;

}

I used the app.config with 3 databases:

<add name="Test" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Test.mdf;Integrated Security=True;User Instance=True"

providerName="System.Data.SqlClient" />

<add name="Demo" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Demo.mdf;Integrated Security=True;User Instance=True"

providerName="System.Data.SqlClient" />

<add name="PTracker" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PTracker.mdf;Integrated Security=True;User Instance=True"

providerName="System.Data.SqlClient" />

I can see that the correct connectionstring is used. It works as I hoped. changing the database name in clientcontext works also. I then get the connectionstring of the database name I have choosen.

But is this still safe code? Just calling for clientcontext["dbName"] on the server-side in database.cs within a propertie? don't these clientcontext from multiple users get mixed up?

 

Ton Smeets replied on Sunday, October 14, 2007

Does this also work with CommandBase objects?

Copyright (c) Marimer LLC