Wednesday, November 16, 2005

Nini: Manual - Storing configuration data in a database

Nini: Manual: "5.2 Storing configuration data in a database

If you are running an ASP.NET application then you probably have multiple users in each system. You will probably also have a bunch of user settings to edit. Most web applications are entirely configured with a database so the following is an example of using Nini along with your database.

Here's a very simple example with a SQL Server table. This can easily be adapted to any other database. Here is the structure of the database table:

CREATE TABLE UserSettings
(
UserId ID,
Settings TEXT
);

The ConfigSettings field stores a Nini configuration value. Now you can load a Nini configuration values like this:

string userId = GetUserId(); // retrieve the user id somehow

SqlCommand command = new SqlCommand('SELECT Settings FROM UserSettings WHERE ID = '
+ userId, connection);

connection.Open();

SqlDataReader reader = command.ExecuteReader();

if(reader.HasRows) {
reader.Read();
IConfigSource source = new XmlConfigSource(new StringReader(reader.GetString(0)));
}

reader.Close();
connection.Close();"
Post a Comment