Print Page | Close Window

Changing the Connection String at Runtime

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3310
Printed Date: 28-Mar-2024 at 9:09am


Topic: Changing the Connection String at Runtime
Posted By: Sakar SR
Subject: Changing the Connection String at Runtime
Date Posted: 01-Mar-2012 at 6:58am
How to change the directory and path of my database location ?

string datapath = @"'D:\SQLite Database\eFillerDB\eFilerDB.DAT";
string metadata = @"metadata= res://*/eFiler.csdl|//*/eFiler.ssdl|res://*/eFiler.msl;
                                provider=System.Data.SQLite;
                                provider connection string='data source='" + datapath;
string conn = metadata;

var config = IdeaBlade.Core.IdeaBladeConfig.Instance;
var keys = config.EdmKeys;
            
keys.Add(
                new EdmKeyElement
                {
                    Name = "eFilerDBEntities",
                    LogTraceString = false
                    Connection = conn                       
                });

When i try to run this code i get an runtime error saying to check the connection string.

my requirement is only to change the path and directory of the database location at runtime . what is wrong in the above code.

Please explain, to correct this problem.







Replies:
Posted By: sbelini
Date Posted: 01-Mar-2012 at 6:13pm
Hi Sakar,
 
It might be possible that your connection is sting is malformed. (I see opening single quotes on datapath, but no closing)
A good starting point would be looking at the connectionString created in your Model's app.config file (i.e. assuming you created your model based on an existing database)
For instance, I've tried the following without any issues: (I've copied the connectionString from app.config)
 
EdmKeyElement edmKey = new EdmKeyElement("NorthwindIBEntities");
edmKey.Connection = "metadata=res://*/NorthwindModel.csdl|res://*/NorthwindModel.ssdl|res://*/NorthwindModel.msl;provider=System.Data.SqlClient;provider connection string='data source=.;initial catalog=NorthwindIB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework'";
IdeaBladeConfig.Instance.EdmKeys.Add(edmKey);
 
Also, you might want to use DataSourceExtensions.
You can find more details about DataSourceExtensions in the http://drc.ideablade.com/xwiki/bin/view/Documentation/create-entitymanager-datasource-extension - DevForce Resource Center .
 
Regards,
   Silvio.


Posted By: Sakar SR
Date Posted: 02-Mar-2012 at 12:07am
Hi Sbelini,

Thank you for the help, i solved the problem

Have a nice day

Regards,
Sakar





Print Page | Close Window