Custom .NET Data Providers

梦想与她 提交于 2020-01-01 02:49:04

问题


Is is possible to use a custom .NET data provider without installing it in the GAC?

Can I reference a custom DLL and register it inside my configuration file?


回答1:


Yes, you can register an implementation of the DbProviderFactory class by adding the following section in your configuration file:

<system.data>
    <DbProviderFactories>
        <add name="My Custom Data Provider"
             invariant="MyCustomDataProvider" 
             description="Data Provider for My Custom Store" 
             type="MyNamespace.MyCustomProviderFactory, MyCustomDataProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=" />
    </DbProviderFactories>
</system.data>

The MyCustomDataProvider assembly doesn't have to be registered in the GAC but can be deployed together with the application as a private assembly.

You can refer to the registered data provider programmatically by using the value specified in the invariant attribute. For example you could tell ADO.NET to use the MyNamespace.MyCustomProviderFactory by specifying MyCustomProvider as the providerName in the connection string:

<connectionStrings>
    <add name="ConnString" 
         providerName="MyCustomProvider" 
         connectionString="MyCustomConnectionString" />
</connectionStrings>

In code you can use the same provider name with the DbProviderFactories.GetFactory method:

DbProviderFactory factory = DbProviderFactories.GetFactory("MyCustomDataProvider");

where factory will be an instance of the MyNamespace.MyCustomProviderFactory class.



来源:https://stackoverflow.com/questions/9467295/custom-net-data-providers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!