Click here to Skip to main content
13,351,243 members (48,867 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 17 May 2006

Custom Resource Reader

, 1 Jun 2006
Rate this:
Please Sign up or sign in to vote.
Implementing custom resource reader with database
Sample Image - Custom_Resource_Reader.jpg


This article would help you to create a custom resource reader if you are starting out. Resources are an application-building feature that allow you to place culture-specific items inside satellite files, rather than directly in your main application. When you build your application, you can identify aspects that are culture-specific, and make a different resource file for each culture where you think your application may be used. At run time, the appropriate set of resources will be loaded, based on the user's culture settings. The specific setting used is the CurrentUICulture for your thread, which the user can set programmatically.

When you make your application, it is housed in an 'assembly'. An assembly consists of the following:

  • The assembly metadata, also called the assembly manifest
  • Type metadata
  • The Microsoft Intermediate Language (MSIL) code that implements the types
  • A set of resources

You may also put these resource files in an external repository like a database. For that, you have to create a custom resource reader for your application.

Using the Code

For this article, language specific resource elements are placed in the Microsoft SQL Server database. Create a SQL Server database with a single table Messages. Its create script would look like this:

CREATE TABLE [dbo].[Messages]
[Id] [int] IDENTITY(1,1) NOT NULL,
[Key] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Default] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[de] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[mn] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

There are three steps to creating a custom resource reader. The first step is to create a class that implements IResourceReader, which requires a Close method and an implementation for GetEnumerator.

public class DBResourceReader:IResourceReader
  //Database Connection
  private string ConnectionString;

  //Language to retrieve
  private string Language;
  public DBResourceReader(string ConnectionString, CultureInfo Culture)
   this.ConnectionString = ConnectionString;
   this.Language = Culture.Name; 
  //To get enumerator to iterate through the resources hashtable
  public IDictionaryEnumerator GetEnumerator() 
   //Hashtable to store Key-values pairs for resources
   Hashtable htLanguage = new Hashtable();
   //SQL DB connection to database
   SqlConnection conn = new SqlConnection(ConnectionString); 
   //Command to select specific language key value
   SqlCommand command = conn.CreateCommand(); 
   if (Language == "") 
      Language = "Default";
      command.CommandText = "Select [key], [" + Language + "] from Messages";
    SqlDataReader reader = command.ExecuteReader(); 
    while (reader.Read()) 
     //Add all the key value pairs for specific language to a hashtable
     if (reader.GetValue(1) != System.DBNull.Value)
         htLanguage.Add(reader.GetString(0), reader.GetString(1)); 
    catch {}
  return htLanguage.GetEnumerator();
 //You may close db connection here.
 public void Close()
 { }
 //dispose resources that are not required
 public void Dispose()
 { }

//Implement IEnumerable interface 
IEnumerator IEnumerable.GetEnumerator()
  return this.GetEnumerator(); 

The next step is to generate a custom resource set. It stores all the resources localized for one particular culture. To do this, create a derived class of DBResourceSet, and override GetDefaultReader:

public class DBResourceSet:ResourceSet
       public DBResourceSet(string ConnectionString, CultureInfo Culture): 
		base(new DBResourceReader(ConnectionString,Culture))
       //Return custom reader as default one for reading language resources 
       public override Type GetDefaultReader()
            return typeof(DBResourceReader);

The next step is to generate a custom resource manager that uses the custom reader. To do this, create a derived class of ResourceManager, and override InternalGetResourceSet to allow for the creation of the resource reader defined earlier. The following code example demonstrates a simple ResourceManager:

public class DBResourceManager : ResourceManager
        //Connection string for database
        private string ConnectionString;
        public DBResourceManager(string ConnectionString)
            this.ConnectionString = ConnectionString;
            //initialize ResourceSets hashtable 
            ResourceSets = new Hashtable(); 
        //ResourceManager.GetString() makes use of this function
        protected override ResourceSet InternalGetResourceSet(
          CultureInfo culture, bool createIfNotExists, bool tryParents)
            DBResourceSet rs = null;
            if (ResourceSets.Contains(culture.Name))
                rs = (DBResourceSet)ResourceSets[culture.Name];
                rs = new DBResourceSet(ConnectionString, culture);
                ResourceSets.Add(culture.Name, rs);
            return rs;


This article explains how to create a custom resource reader by using SQL Server database as resource repository for storing localized resources. You can also implement resource reader interface by using a different resource repository.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Palwinder Singh
Team Leader
India India
Techinical Lead on Microsoft technologies.Also,worked with PHP/ MYSQL. My core expertise is on dot net framework 2.0,3.5 and 4.0.

You may also be interested in...


Comments and Discussions

Questionchange database Pin
Cool Smith3-Sep-17 3:02
memberCool Smith3-Sep-17 3:02 
QuestionFor other resources like images I want to read from resx...Is it possible? Pin
Suyash Kunte15-Jul-10 3:10
memberSuyash Kunte15-Jul-10 3:10 
QuestionHow can I use this example in a ASP.NET Project? Pin
asbscout19-Oct-06 5:59
memberasbscout19-Oct-06 5:59 
AnswerRe: How can I use this example in a ASP.NET Project? Pin
Tomaž Štih13-May-13 21:38
memberTomaž Štih13-May-13 21:38 
GeneralCool!!! Pin
MicrosoftBob1-Jun-06 7:58
memberMicrosoftBob1-Jun-06 7:58 
GeneralRe: Cool!!! Pin
Palwinder Singh14-Jul-08 0:03
memberPalwinder Singh14-Jul-08 0:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 1 Jun 2006
Article Copyright 2006 by Palwinder Singh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid