实现自己的ProviderBase

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

转载于:https://www.cnblogs.com/lin614/archive/2011/05/18/2049739.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
System.Data.OleDb.OleDbException HResult=0x80004005 Message=外部表不是预期的格式。 Source=System.Data StackTrace: at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.OleDb.OleDbConnection.Open() at excl.Form1.TransferData(String excelFile, String sheetName, String connectionString) in G:\c++通讯录\excl\Form1.cs:line 39 此异常最初是在此调用堆栈中引发的: System.Data.OleDb.OleDbConnectionInternal.OleDbConnectionInternal(System.Data.OleDb.OleDbConnectionString, System.Data.OleDb.OleDbConnection) System.Data.OleDb.OleDbConnectionFactory.CreateConnection(System.Data.Common.DbConnectionOptions, System.Data.Common.DbConnectionPoolKey, object, System.Data.ProviderBase.DbConnectionPool, System.Data.Common.DbConnection) System.Data.ProviderBase.DbConnectionFactory.CreateConnection(System.Data.Common.DbConnectionOptions, System.Data.Common.DbConnectionPoolKey, object, System.Data.ProviderBase.DbConnectionPool, System.Data.Common.DbConnection, System.Data.Common.DbConnectionOptions) System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionPoolGroup, System.Data.Common.DbConnectionOptions) System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(System.Data.Common.DbConnection, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal, out System.Data.ProviderBase.DbConnectionInternal) System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions) System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions) System.Data.ProviderBase.DbConnectionInternal.OpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory) System.Data.OleDb.OleDbConnection.Open() excl.Form1.TransferData(string, string, string) (位于 Form1.cs 中)
最新发布
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值