0

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

3
  • Just make it a relative path to the database file, as opposed to absolute. Commented Feb 5, 2014 at 16:30
  • sir a little bit elaboration please? Commented Feb 5, 2014 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) Commented Feb 5, 2014 at 16:33

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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
    }

4 Comments

same problem if i change it to D: drive then i have to change rootPath="D:\";
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.
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
Good job. Will remember that for the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.