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
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
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.
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")
Ben.
ASKER
Yes, my sql server database file is in the App_Data folder,
so to use it what should i write in the AttachDbFilename=??
so to use it what should i write in the AttachDbFilename=??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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="NORTHWNDConnectionSt ring" connectionString="Data Source=.\SQLEXPRESS;Attach DbFilename =|DataDire ctory|\NOR THWND.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
i have downloaded a sample application from internet
and in this applicaton here is connectionstring defined in the web.config file
<add name="NORTHWNDConnectionSt
</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
ASKER
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
actually in the connectionstring given in the web config file has one"\" before the datbase file name
=|DataDirectory|\NORTHWND.
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="NORTHWNDConnectionSt ring" connectionString="Data Source=.\SQLEXPRESS;Attach DbFilename =|DataDire ctory|\NOR THWND.MDF; Database=N orthwind;I ntegrated Security=True;" providerName="System.Data. SqlClient" />
Hope this makes sense.
Ben.
So the setup should be
webroot\App_Data\NORTHWIND
Use this connection string
<add name="NORTHWNDConnectionSt
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.
Ben.
>> want to give a relative path
i think its not required to path to datafiles. you just need server name, user id and password.