Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have developed an ASP.net web application which interacts with sqlserver database. For database related task like ADO.net. Connection string gets loaded from web.config file. connection string loading code is written below

 public DataBaseCache()
        {
            CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
          //etc
        }

My web.config file is below

<connectionStrings>
    <add name="DBCS"
         connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=F:\ProjectApplication6-3\ProjectApplication\App_Data\ProjectDatabases.mdf;Integrated Security=True;User Instance=True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

Now Problem is that if i save my Visual studio project file to other drive i need to change my connection string in web.config file in this example it is in Drive F. Please guide me how to avoid this copy paste each time i save application to various drives and code does it automatically.? regards

share|improve this question
    
Just make it a relative path to the database file, as opposed to absolute. –  mason Feb 5 at 16:30
    
sir a little bit elaboration please? –  user3266922 Feb 5 at 16:31
    
You are using an entire path to say where your database file is located. Instead, you just need to include directions on how to get to it from your project. See Path article on Wikipedia. en.wikipedia.org/wiki/Path_(computing) –  mason Feb 5 at 16:33

2 Answers 2

up vote 1 down vote accepted

Put the database in the App_Data directory in your project and use:

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

When you move your project to a different drive/computer, and SQL Express is installed, your project should be able to attach to your database.

This other question is similar to yours and may provide additional insight.

Note I added "|DataDirectory|" to the connection string

share|improve this answer
    
Thanks Dear Sir –  user3266922 Feb 5 at 17:09

change your web.config

<connectionStrings>
<add name="DBCS"
     connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename={0}ProjectApplication6-3\ProjectApplication\App_Data\ProjectDatabases.mdf;Integrated Security=True;User Instance=True"
     providerName="System.Data.SqlClient" />

and then in your code

public DataBaseCache()
    {
        string rootPath="F:\";
        CS = String.Format(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString, rootPath);

      //etc
    }
share|improve this answer
    
same problem if i change it to D: drive then i have to change rootPath="D:\"; –  user3266922 Feb 5 at 16:38
    
You can set it from the global.asax.cs file. From within the Application_Start event call Context.Request.PhysicalApplicationPath or from elsewhere you may get it from HttpContext.Current.Request.PhysicalApplicationPath. Obviously you will need to recover the drive letter from the full path. –  rob Feb 5 at 16:49
1  
I found the solution my self it is just using |DataDirecory| instead of full path since |DataDirectory| points to Appdata folder and then u just need to use /databaseName.mdf after it.Hope it will be beneficial to u also –  user3266922 Feb 5 at 16:54
    
Good job. Will remember that for the future –  rob Feb 5 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.