DatabaseResourceReader.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:1k
- using System;
- using System.Resources;
- using System.Globalization;
- using System.Collections;
- using System.Data.SqlClient;
- namespace Wrox.ProCSharp.Localization
- {
- public class DatabaseResourceReader : IResourceReader
- {
- private string dsn;
- private string language;
- public DatabaseResourceReader(string dsn, CultureInfo culture)
- {
- this.dsn = dsn;
- this.language = culture.Name;
- }
- public System.Collections.IDictionaryEnumerator GetEnumerator()
- {
- Hashtable dict = new Hashtable();
- SqlConnection connection = new SqlConnection(dsn);
- SqlCommand command = connection.CreateCommand();
- if (language == "")
- language = "Default";
- command.CommandText = "SELECT [key], [" + language + "] " +
- "FROM Messages";
- try
- {
- connection.Open();
- SqlDataReader reader = command.ExecuteReader();
- while (reader.Read())
- {
- if (reader.GetValue(1) != System.DBNull.Value)
- dict.Add(reader.GetString(0), reader.GetString(1));
- }
- reader.Close();
- }
- catch // ignore missing columns in the database
- {
- }
- finally
- {
- connection.Close();
- }
- return dict.GetEnumerator();
- }
- public void Close()
- {
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- void IDisposable.Dispose()
- {
- }
- }
- }