|
The following method is based on storing application settings in database.
This method is most flexible and independent from .NET infrastructure, however it requires additional system resources for the performing of database operations.
Review the following example:
SQL Script
/*==============================================================*/
/* Table: Settings */
/*==============================================================*/
create table t_settings (
id int not null,
description nvarchar(255) not null,
value nvarchar(255) not null
)
on PRIMARY
go
insert into t_settings values(1, "Site administrator name", "admin")
insert into t_settings values(2, "Site administrator password", "adminPWD")
insert into t_settings values(3, "Images directory", "img")
Configurator.cs
public enum Settings
{
AdminName = 1,
AdminPassword = 2,
ImagesDir = 3
}
public string GetConfigurationValue(Settings setting)
{
SqlConnection myCnn;
SqlCommand myCmd;
SqlDataReader myReader;
String sSQL = "select * from t_settings where id=" + setting;
myCnn = new SqlConnection(sSQLConnectionString);
myCmd = new SqlCommand(sSQL, myCnn);
myCnn.Open();
myReader = myCmd.ExecuteReader();
myReader.Read();
sSQL = myReader["value"];
myCnn.Close();
return sSQL;
}
SomePage.asax.cs
private void Page_Load(object sender, System.EventArgs e)
{
Configuration conf = new Configuration();
sImagePath = conf.GetConfigurationValue(Settings.ImagesDir);
// :
// Do something
// :
}
An of course let's consider the advantages and disadvantages of using database as a storage for web application parameters.
Advantages:
- Centralized storage of all application settings
- Settings can be operatively changed
- No need in recompilation of whole application
- Application restart doesn't occur after the changing of settings
Disadvantages:
- Database engine is required
- Additional system resources for database operations
- General reliability of application decreases
- Anyone, who has an access to database, can change application settings
|