Dyanically changing connection strings for SqlDataSource

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm working on a web app that is published to a hosting service. I'm
developing it on my local PC with VS 2005 and SQL Express. The hosting
service DB is SQL Server 2000.

I have two connectionStrings defined in my web.config file, one for the
development db and another for the production db.

When I publish my app to the hosting site, I rename the two connection
strings so that the production string will be the active string in the
production environment.

I'd rather not have to change strings back and forth to make my app work in
either environment.

Is there a best practice for this type of situation?

My app uses a lot of SqlDataSource controls. Is there a way to dynamically
change the name of the connectionString, defined in the SqlDataSource control
at run-time?

Thanks in advance
Matt
 
Hi Matt,

why would you like to publish the web.config file every time?
Personally, I setup a web.config file for development use
(local connection strings, eventually debugging and tracing
turned on) and another one for production use (production
connection strings, debug turned off).

When deploying, only *.as?x and *.dll are uploaded to the
server.

If you should need one web.config file, which in published
together with the app, maybe adding the computer name to
the connection strings would be an option.

<connectionStrings>
<add name="conn.DEV" connectionString="..." />
<add name="conn.LIVE" connectionString="..." />
</connectionStrings>

When accessing the connection strings, just use the
corresponding connection string. The local machine name
can be accessed by System.Environment.MachineName
Would be something like

string connStringName = String.Format( "conn.{0}",
System.Environment.MachineName.ToUpper() );


Regards,
Jens Rieck
http://www.jens-rieck.de
 
If I could "publish" without uploading the web.config, that would be ideal.
I was under the impression that the "publish web" function in VS 2005, pushed
a fresh copy of everything to the web. How would I tell it which files not
to publish?

Matt
 
Back
Top