Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. URGENT: How to solve on hard-code the database file path?

URGENT: How to solve on hard-code the database file path?

Scheduled Pinned Locked Moved ASP.NET
questiondatabasetutorial
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DotNet
    wrote on last edited by
    #1

    Hie... Currently, this is what I declared as below: I know is not a good thing to hard code the path mentioned. So, how can I solve it? Regards, Aaron

    C D 2 Replies Last reply
    0
    • D DotNet

      Hie... Currently, this is what I declared as below: I know is not a good thing to hard code the path mentioned. So, how can I solve it? Regards, Aaron

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Put it in the web.config


      Do you want to know more?


      Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.

      1 Reply Last reply
      0
      • D DotNet

        Hie... Currently, this is what I declared as below: I know is not a good thing to hard code the path mentioned. So, how can I solve it? Regards, Aaron

        D Offline
        D Offline
        DotNet
        wrote on last edited by
        #3

        Opps... some thing can't display here. It is the access database file path here. Hope to hear from you guys soon. Thank you very much.

        C 1 Reply Last reply
        0
        • D DotNet

          Opps... some thing can't display here. It is the access database file path here. Hope to hear from you guys soon. Thank you very much.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Sorry, I don't respond privately in case others have the same problem and are searching the boards for an answer. I've made the reply generic so it does not expose the information you sent to me. You place the information in the web.config file, that way if your database file moves you can just update the web.config file in a text editor. It is not compiled into your application and ASP.NET will monitor the file constantly for changes, as soon as it detects a change it will be reflected in your web application almost instantly. I think you've already figured this bit out for yourself, but show it just in case. In you web.config you would then have an appSetting section that might look something like this:

          <appSettings>
          <add key="DataBasePath" value="c:\myDatabaseFile.dat"/>
          </appSettings>

          Then in your code, you can retrieve the location with

          using System.Configuration;

          string path = ConfigurationSettings.AppSettings["DataBasePath"];

          It would actaully be better to create a class that retrieves this information. e.g.

          public class AppConfig
          {
          public static string DatabaseLocation
          {
          get
          {
          return ConfigurationSettings.AppSettings["DataBasePath"];
          }
          }
          }

          This would also allow you to create a property that gets the database connection string built from the config settings that is consistent across your application. For instance:

          // This is created inside the AppConfig class created above.
          public static string ConnectionString
          {
          get
          {
          // replace the a and b characters with the relevant portions
          // of the connection string.
          return string.Format("aaaaaaaa{0}bbbbbbbb", DatabaseLocation);
          }
          }

          Other than that, I really can't see how much more dynamic you want it. For instance: A really big project that I worked on about a year ago, which needed configiguration information shared between Windows machines and UNIX machines put all that information in a central configuration database so that the only thing that needed to be duplicatated across each machine/application was a line telling it where to find the central database. Does this help?


          Do you want to know more?


          Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity

          D 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Sorry, I don't respond privately in case others have the same problem and are searching the boards for an answer. I've made the reply generic so it does not expose the information you sent to me. You place the information in the web.config file, that way if your database file moves you can just update the web.config file in a text editor. It is not compiled into your application and ASP.NET will monitor the file constantly for changes, as soon as it detects a change it will be reflected in your web application almost instantly. I think you've already figured this bit out for yourself, but show it just in case. In you web.config you would then have an appSetting section that might look something like this:

            <appSettings>
            <add key="DataBasePath" value="c:\myDatabaseFile.dat"/>
            </appSettings>

            Then in your code, you can retrieve the location with

            using System.Configuration;

            string path = ConfigurationSettings.AppSettings["DataBasePath"];

            It would actaully be better to create a class that retrieves this information. e.g.

            public class AppConfig
            {
            public static string DatabaseLocation
            {
            get
            {
            return ConfigurationSettings.AppSettings["DataBasePath"];
            }
            }
            }

            This would also allow you to create a property that gets the database connection string built from the config settings that is consistent across your application. For instance:

            // This is created inside the AppConfig class created above.
            public static string ConnectionString
            {
            get
            {
            // replace the a and b characters with the relevant portions
            // of the connection string.
            return string.Format("aaaaaaaa{0}bbbbbbbb", DatabaseLocation);
            }
            }

            Other than that, I really can't see how much more dynamic you want it. For instance: A really big project that I worked on about a year ago, which needed configiguration information shared between Windows machines and UNIX machines put all that information in a central configuration database so that the only thing that needed to be duplicatated across each machine/application was a line telling it where to find the central database. Does this help?


            Do you want to know more?


            Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity

            D Offline
            D Offline
            DotNet
            wrote on last edited by
            #5

            Well, I had put targeted file path location in web config. Come to this part, value="C:\Inetpub\wwwroot\abc\12345.mdb". When I declare as C:\ , I already hard code it. So what if, I want to set my database file path dynamically? What if I put my web application into D:\ or E:\ ?

            C 1 Reply Last reply
            0
            • D DotNet

              Well, I had put targeted file path location in web config. Come to this part, value="C:\Inetpub\wwwroot\abc\12345.mdb". When I declare as C:\ , I already hard code it. So what if, I want to set my database file path dynamically? What if I put my web application into D:\ or E:\ ?

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              DotNet wrote: So what if, I want to set my database file path dynamically? Use Request.PhysicalApplicationPath[^] and then concatenate the remainder of the filename to the end of it. However, I really do not recommend putting your database inside a directory structure that is being exposed as part of a web application, as a minor lapse in security could expose your database to the open, especially as when you move your application from one drive to another you will have to set the security on the new directories. If you forget to protect your database....


              Do you want to know more?


              Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.

              L 1 Reply Last reply
              0
              • C Colin Angus Mackay

                DotNet wrote: So what if, I want to set my database file path dynamically? Use Request.PhysicalApplicationPath[^] and then concatenate the remainder of the filename to the end of it. However, I really do not recommend putting your database inside a directory structure that is being exposed as part of a web application, as a minor lapse in security could expose your database to the open, especially as when you move your application from one drive to another you will have to set the security on the new directories. If you forget to protect your database....


                Do you want to know more?


                Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                That is correct. The best way in my opinion is to use your own DB-config file (XML for instance) for storing a database path, connection string, etc for your ASP.NET applications. That way you can change it easier when you need without hard-coding it and re-compiling your solution. I would also recommend an encryption to that as well. Microsoft has a good example for ASP.NET applications security right here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT11.asp[^] Niklas Henricson

                S 1 Reply Last reply
                0
                • L Lost User

                  That is correct. The best way in my opinion is to use your own DB-config file (XML for instance) for storing a database path, connection string, etc for your ASP.NET applications. That way you can change it easier when you need without hard-coding it and re-compiling your solution. I would also recommend an encryption to that as well. Microsoft has a good example for ASP.NET applications security right here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT11.asp[^] Niklas Henricson

                  S Offline
                  S Offline
                  shambho
                  wrote on last edited by
                  #8

                  I feel using resource file is a good way of doing it as web.config is available only for web applications or even a raw xml file can be used. anand

                  J 1 Reply Last reply
                  0
                  • S shambho

                    I feel using resource file is a good way of doing it as web.config is available only for web applications or even a raw xml file can be used. anand

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    app.config for all other apps?? Putting config in a straight xml file feels like re-invention of the wheel to me - .NET provides some great libraries for dealing with .config files.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups