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

user3266922
  • 73
  • 1
  • 7
  • Just make it a relative path to the database file, as opposed to absolute. – mason Feb 05 '14 at 16:30
  • sir a little bit elaboration please? – user3266922 Feb 05 '14 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. http://en.wikipedia.org/wiki/Path_(computing) – mason Feb 05 '14 at 16:33

2 Answers2

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

Community
  • 1
  • 1
FrankO
  • 2,522
  • 6
  • 24
  • 34
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
    }
rob
  • 8,134
  • 8
  • 58
  • 68
  • same problem if i change it to D: drive then i have to change rootPath="D:\"; – user3266922 Feb 05 '14 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 05 '14 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 05 '14 at 16:54
  • Good job. Will remember that for the future – rob Feb 05 '14 at 17:00