URGENT: How to solve on hard-code the database file path?
-
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
Put it in the web.config
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.
-
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
-
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.
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?
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
-
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?
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
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:\ ?
-
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:\ ?
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....
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.
-
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....
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.
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
-
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
-
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