Database Connection String Syntax

  • Thread starter Will Chamberlain
  • Start date
W

Will Chamberlain

This may seem like a very novice question but I am having trouble with
it. I created an application that was hosted on a webserver where the
database location happened to be c:\Inetpub\wwwroot\db. For reasons
beyond my control the application folder is going to be moved to a
different folder and reside in a different directory structure. The
connection string below worked perfect on the last server.

<add key="strConnection" value="Provider=Microsoft.Jet.OleDb.4.0;Data
Source=C:\Inetpub\wwwroot\DrawingList\db\db1.mdb;"/>

How would I edit this to reflect a change in drive letter, such as E, D,
or wherever the application gets moved to. I have no say in where this
application goes. Any suggestions.
 
C

Christiaan van Bergen

Hi Will,

Why not rewrite the connectionstring at runtime?
As long as the path of your database does not change relative to your
application, just ask for the current fysical path of the application using

Server.MapPath(".")

This method will return the fysical path of the virtual path you supply.
E.g. Server.MapPath("data") might return c:\inetpub\wwwroot\website\data\
or Server.MapPath(".") might return c:\inetpub\wwwroot\website\

As soon as you know the fysical path, rewrite the "Data Source" section in
your connectionstring.

Hope this helps

Cheers
Christiaan van Bergen
 
W

Will Chamberlain

Thanks for the reply Christiaan. The only problem is that Server.MapPath
can't be used in the web.config. The path needs to be static.

- will
 
C

Christiaan van Bergen

Hi Will,

Yes, I know. You could however place it for instance in the global.asax and
place the connectionstring in for example an application- or session
variable. This way you can use this variable instead of the value placed in
the web.config

In what way are you using the connectionstring in your application?
You could use it as follows:

SqlConnection oConn = new SqlConnection( Session["connectionstring"] as
string );


Christiaan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top