林子之大.net笔记

本站多数文章由其它网址转载,没啥技术含量的皆为原创。主要目的是对付记性不好,方便查询。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
imageprovider类,自己的Provider
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Configuration.Provider;
using System.Drawing;
namespace ps
{
     public abstract class ImageProvider : ProviderBase
     {

         public abstract Image ShowImg(string id);
     }
}
ImageProviderCollection类,ImageProvider类的集合
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Configuration.Provider;
namespace ps
{
     public class ImageProviderCollection:ProviderCollection
     {
         public new ImageProvider this[string name]
         {
             get
             {
                 return (ImageProvider)base[name];
             }
         }
         public override void Add(ProviderBase provider)
         {
             if (provider == null)
             {
                 throw new ArgumentNullException("provider is null");
             }
             if (!(provider is ImageProvider))
             {
                 throw new ArgumentException("provider is not imageprovider");
             }
             base.Add(provider);
         }
     }
}
ImageServiceSection类,实现自己的Section
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Configuration.Provider;
namespace ps
{
     public class ImageServiceSection:ConfigurationSection
     {
         [ConfigurationProperty("myp")]
         public ProviderSettingsCollection provider
         {
             get
             {
                 return (ProviderSettingsCollection)base["myp"];
             }
         }
         [StringValidator(MinLength = 1)]
         [ConfigurationProperty("defaultProvider", DefaultValue = "SqlImageProvider")]
         public string defaultProvider
         {
             get
             {
                 return (string)base["defaultProvider"];
             }
             set
             {
                 base["defaultProvider"] = value;
             }
         }
     }
}
SqlImg类,具体实现自己的工作
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
namespace ps
{
     public class SqlImg : ImageProvider
     {
         private string _constr;
         public override Image ShowImg(string id)
         {
             System.Data.SqlClient.SqlConnection conn = new SqlConnection(_constr);
             string sql = "select url from img where id =" + id;
             System.Data.SqlClient.SqlCommand cmd = new SqlCommand(sql, conn);
             cmd.Connection.Open();
             //cmd.ExecuteScalar();
             System.Data.SqlClient.SqlDataReader read = cmd.ExecuteReader();
             if (read.Read())
             {
                 string url = read[0].ToString();
                 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(url);
                 return (System.Drawing.Image)bmp;
             }
             else
             {
                 return null;
             }
             //throw new Exception("The method or operation is not implemented.");
         }
         public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
         {
             base.Initialize(name, config);
             string connfig = config["sqlconstr"];
             _constr = System.Configuration.ConfigurationManager.ConnectionStrings[connfig].ConnectionString;
         }
     }
}
ImageService类,对自己实现的sqlimg进行调用
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Configuration.Provider;
using System.Drawing;
using System.Drawing.Imaging;
namespace ps
{
     public class ImageService
     {
         private static ImageProvider _ip;
         private static ImageProviderCollection _ipc;
         private static object _obj = new object();
         public static ImageProvider ip
         {
             get
             {
                 loadprovider();
                 return _ip;
             }
         }
           private static void loadprovider()
         {
             if (_ipc == null)
             {
                 lock (_obj)
                 {
                     ImageServiceSection section = (ImageServiceSection)ConfigurationManager.GetSection("testmy");
                     _ipc = new ImageProviderCollection();
                     System.Web.Configuration.ProvidersHelper.InstantiateProviders(section.provider, _ipc, typeof(SqlImg));
                     _ip = _ipc[section.defaultProvider];

                 }
             }
         }
     }
}
然后看看confi文件的配置
<configSections>
           <section name="testmy" type="ps.ImageServiceSection,ps"   allowDefinition="MachineToApplication"   restartOnExternalChanges="true" />
   </configSections>
     <appSettings/>
   <connectionStrings>
     <add name="ImageServiceConnectionString" connectionString="server=PM_WANGCHAO_PC\SQLEXPRESS;database=test;uid=sa;pwd=123" providerName="System.Data.SqlClient"/>
   </connectionStrings>
<testmy defaultProvider="SqlImageProvider">
     <myp>
       <add name="SqlImageProvider" type="ps.SqlImg,ps"   sqlconstr="ImageServiceConnectionString"/>
     </myp>
   </testmy>
代码贴完了,总的来说比较简单。相信大家对配置驱动又会加深理解老。

posted on 2011-05-18 11:29  林614  阅读(496)  评论(0编辑  收藏  举报