与SQL CE 3.5,4.0,4.0.0.1无关的版本SqlCeConnection.InfoMessage事件 [英] Version independent SqlCeConnection.InfoMessage Event for SQL CE 3.5, 4.0, 4.0.0.1
问题描述
如何为SQL Server CE进行版本无关的InfoMessage事件处理?
我有一个输入连接作为DbConnection。
另外项目包括SQL Server CE 4.0.1程序集作为私有部署。
以下代码:
How to make version independent InfoMessage event handling for SQL Server CE?
I have an input connection as DbConnection.
Also the project includes SQL Server CE 4.0.1 assemblies as private deployment.
The following code:
ByVal connection As DbConnection
With CType(connection, SqlCeConnection)
RemoveHandler .InfoMessage, AddressOf SqlCeInfoMessageHandler
AddHandler .InfoMessage, AddressOf SqlCeInfoMessageHandler
End With
引发异常:
raises exception:
[A]System.Data.SqlServerCe.SqlCeConnection cannot be cast to [B]System.Data.SqlServerCe.SqlCeConnection. Type A originates from 'System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\Windows\assembly\GAC_MSIL\System.Data.SqlServerCe\3.5.1.0__89845dcd8080cc91\System.Data.SqlServerCe.dll'. Type B originates from 'System.Data.SqlServerCe, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\Users\...\System.Data.SqlServerCe.dll'.
DbConnection没有InfoMessage事件。所以必须使用SqlCeConnection类。
DbConnection has no InfoMessage event. So the SqlCeConnection class must be used.
推荐答案
我已经解决了这个任务。
I've solved the task.
Imports System.Reflection
Dim eventInfo As EventInfo = connection.GetType.GetEvent("InfoMessage")
Dim eventType As Type = eventInfo.EventHandlerType
Dim handlerInfo As MethodInfo = Me.GetType.GetMethod("InfoMessageHandler")
Dim handler As [Delegate] = [Delegate].CreateDelegate(eventType, Me, handlerInfo)
eventInfo.RemoveEventHandler(connection, handler)
eventInfo.AddEventHandler(connection, handler)
此代码不需要导入System.Data .SqlServerCe.SqlCeConnection命名空间,适用于任何SQL Server Compact版本。
This code do not require importing the System.Data.SqlServerCe.SqlCeConnection namespace and works with any SQL Server Compact version.
这篇关于与SQL CE 3.5,4.0,4.0.0.1无关的版本SqlCeConnection.InfoMessage事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!