Link to home
Start Free TrialLog in
Avatar of ziorrinfotech
ziorrinfotech

asked on

how to relative path in connectionstring in web.config file

i have server 2005 database file and i am writing the connectionstring in the web.config file.

I want to give a relative path in the connectionstrin instead of full so that whereever i take this project, it will work with out making any modification in the connectionstring
Avatar of appari
appari
Flag of India image

can you post the existing connection string?
>> want to give a relative path
i think its not required to path to datafiles. you just need server name, user id and password.
Avatar of DBAduck - Ben Miller
It sounds like you are trying to use SQL Express with a datafile, and if that is the case, then you will be using the App_Data folder and should not use any other folder for the data file.

If you are using something like Access and want to use a relative path, then you could always use ~/path/datafile and see if that works.  I have not done this with any other database, because I am usually using SQL Server so I don't need a path, but it is worth a shot.

I know that in code (not in connection strings in web.config) that you can use MapPath("~/path/file.ext") and get the relative path turned into the physical path.

Ben.
Avatar of ziorrinfotech
ziorrinfotech

ASKER

Yes, my sql server database file is in the App_Data folder,
so to use it what should i write in the AttachDbFilename=??
ASKER CERTIFIED SOLUTION
Avatar of DBAduck - Ben Miller
DBAduck - Ben Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the thing which disturbs me
i have downloaded a sample application from internet
and in this applicaton here is connectionstring defined in the web.config file

<add name="NORTHWNDConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>

but when i run this application then it gives me timeout error.
and if i change the connectionstring and give the full path then it work fine.

Am i doing some thing wrong here
Good News i found the mistake,

actually in the connectionstring given in the web config file has one"\" before the datbase file name
=|DataDirectory|\NORTHWND.MDF

when i saw the string given by Ben then i removed this extra \ and then it works file
thanks
for your help and time
You may want to add Database=Northwind.  I am also not sure about the User Instance=True which means that it creates a new instance of SQL Server and that has to be enabled in the .\SQLEXPRESS instance, so I would take that out and see if that helps.

So the setup should be
webroot\App_Data\NORTHWIND.MDF
Use this connection string
<add name="NORTHWNDConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Database=Northwind;Integrated Security=True;" providerName="System.Data.SqlClient"/>

Hope this makes sense.
Ben.
Glad it worked out.  I was a little confused why they had the extra \ in the path, and it looks like that was it.

Ben.