⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databaseresourcereader.cs

📁 C#高级编程第6版随书源代码 值得下载
💻 CS
字号:
#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Globalization;
using System.Resources;


#endregion

namespace Wrox.ProCSharp.Localization
{
   public class DatabaseResourceReader : IResourceReader
   {
      private string connectionString;
      private string language;

      public DatabaseResourceReader(string connectionString, CultureInfo culture)
      {
         this.connectionString = connectionString;
         this.language = culture.Name;
      }
      public System.Collections.IDictionaryEnumerator GetEnumerator()
      {
         Dictionary<string, string> dict = new Dictionary<string, string>();

         SqlConnection connection = new SqlConnection(connectionString);
         SqlCommand command = connection.CreateCommand();
         if (String.IsNullOrEmpty(language))
            language = "Default";

         command.CommandText = "SELECT [key], [" + language + "] " +
            "FROM Messages";

         try
         {
            connection.Open();

            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            while (reader.Read())
            {
               if (reader.GetValue(1) != System.DBNull.Value)
                  dict.Add(reader.GetString(0).Trim(), reader.GetString(1));
            }
            Trace.WriteLine(String.Format("read resource for {0}", language));

            reader.Close();
         }
         catch (SqlException ex)	// ignore missing columns in the database
         {
            if (ex.Number != 207)
               throw;
//            Trace.WriteLine(ex.Message);
         }

         return dict.GetEnumerator();
      }

      public void Close()
      {
      }

      IEnumerator IEnumerable.GetEnumerator()
      {
         return this.GetEnumerator();
      }

      void IDisposable.Dispose()
      {
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -