惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园 - 叶小钗
小众软件
小众软件
WordPress大学
WordPress大学
I
InfoQ
Last Week in AI
Last Week in AI
Vercel News
Vercel News
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
腾讯CDC
D
DataBreaches.Net
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 林614

IIS问题:Server Application Error 的解决 UML建模的要点总结 软件企业致胜的18种成功模式 自定义协议的注册及程序示例(C#) Remoting事件处理全接触 Marshal、Disconnect与生命周期以及跟踪服务 Net Remoting基础篇 在没有 IIS 的条件下运行 ASMX ASP.NET MVC学习笔记 C#位运算 匹配css 样式的正则表达式 System.Web.Mvc.HtmlHelper学习及应用 正则表达式提取HTML页面的特定部分 - 林614 - 博客园 服务器应用程序不可用的解决方法 - 林614 - 博客园 体验.NET2.0的优雅之Provider应用 最窄770px最宽1024px经典布局 调试找不到文件的解决方法 FindControl查找内容页上的某个控件 - 林614 - 博客园 遍历页面所有控件的方式
实现自己的ProviderBase
林614 · 2011-05-18 · via 博客园 - 林614

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>
代码贴完了,总的来说比较简单。相信大家对配置驱动又会加深理解老。