Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to change connection to the database at runtime

$
0
0

This example shares some code that can be used to promote your application to another database after the application is already started. To accomplish this task, we have implemented two platform-dependent controllers that have the ChangeDatabase Action.

See also:
Database: How to show Business Classes from several databases in a XAF application

Question Comments

Added By: Mario Blatarić at: 11/5/2012 11:23:09 AM    

Let me give you "some reasons" why this isn't such a specific scenario.
 - accountant company doing accounting for several different companies (it's business requirement that data for each company is in separate database)
 - building maintenance company - again - business requirements demands each building to live in separate database (because of financial data)
 - document managment companies doing document management for multiple companies - again, each company must live in seperate database
 - local community combined with utility company - same people do both things and want to keep things separated

If you like, I can go on. No, you can't go to this companies / communities and say "But, it's better if we put all this to one database" because they will reply "Thank you, we'll find someone who can do it with multiple databases".
The point is, there are many reason why one piece of software should be connecting to multiple databases, so it's not such a specific scenario really. It would be nice to have this functionality out of the box instead of implementing this hard to understand piece of code.
Also, since application is distributed to many different clients, it would be nice that end-user could setup application on first startup (add/create required databases with connection info).

Added By: Mario Blatarić at: 3/14/2013 9:51:50 AM    

This example doesn't work.
Go to WinChangeDatabaseController.cs and set breakpoint in changeDatabaseAction_Execute. This code never executes since created SingleChoiceAction is not visible on logon form, instead some default editor is created with PredefinedValues and it's behaviour is not modified.
Can you fix this code please?

Added By: Dennis (DevExpress Support) at: 3/28/2013 4:52:33 AM    

@Mario: I have answered you in www.devexpress.com/issue=Q484359
This example worked correctly in my tests. In any event, I have just updated it to .NET Framework 4.0, just for more convenience.

Added By: Mario Blatarić at: 3/28/2013 6:17:17 AM    

Thanks Dennis. I understand now how this works.

Added By: lekan odejimi at: 1/29/2014 1:28:06 AM    

Hi Dennis, each time i try to logon i keep on getting this error: "Make sure your user name is correct and retype the password in the correct case." What do you suggest?

Added By: Grégoire Perruchoud at: 3/20/2014 9:52:00 AM    

Hi,

Great piece of code !

I noticed a weakness. In Web/Win controllers, preselection of correct item is done via following code :
if (Application.ConnectionString.Contains((string)item.Data))

This fails in case database name's string is contains elsewhere in ConnectionString. It also fails in case we have multiple databases with names containing each other, for example "customers" and "customers_geneva".

We should store the current database name in a static property somewhere and make a strong comparison.

Added By: Achmad Mulyadi at: 8/15/2015 12:11:19 AM    

Hi Dennis,

From the code below,

public const string Databases = "ChangeDatabase_DB1;ChangeDatabase_DB2";

Is there anyway I can pass a dynamic attribute value? I know this attribute requires a fixed value in compile time, but I don't think it is practical to put the database name or maybe a server address in a hard coded way. For example, it would be very practical if we can read this value from the app.config file, and pass it to the ModelDefault attribute.

Added By: Dennis (DevExpress Support) at: 8/17/2015 8:12:50 AM    @Achmad: Sure, please check out a possible solution at How to make example E1344 work with runtime defined databases. I hope this meets your needs.Added By: Robert Sanford at: 12/21/2015 3:36:59 PM    

Hi Dennis,
Would it be possible to post a complete XAF solution that incorporates this logic please along with tutorial? I have a single database XAF app that now has multiple database requirement - the various sql server databases would be defined in web.config. Thanks


Viewing all articles
Browse latest Browse all 7205

Trending Articles