0

我正在尝试将 MiniProfiler 添加到我的 DB2 连接中。下面是我的简化代码。

public void InitializeConnection()
{
    DB2Connection cnn = new DB2Connection("connection String");
    var profiler = 
         new StackExchange.Profiling.Data.ProfiledDbConnection(cnn, MiniProfiler.Current);
    IDbCommand c = new DB2Command();
    c.Connection = profiler ;
}

我的问题出现在将分析器分配给 DB2Command 的 Connection 属性的最后一行。我收到以下错误。

无法将“StackExchange.Profiling.Data.ProfiledDbConnection”类型的对象转换为“IBM.Data.DB2.DB2Connection”类型我尝试了几种不同的转换想法,但都没有成功。

4

2 回答 2

0

DB2Command.Connection属性属于类型DB2Connection(正如错误消息有助于告诉您的那样)。请尝试DbConnection

c.DbConnection = profiler

手册中的更多内容

于 2016-06-01T19:06:47.170 回答
0

我认为你在倒退。您正在为班级分配连接(根据MiniProfiler 网站上的文档ProfiledDbConnection,这似乎是正确的)。

但是,您随后将创建一个特定于 DB2 的命令对象,并尝试将ProfiledDbConnection类分配给连接对象。

我认为您想要做的是 call profiler.CreateDbCommand(),它将创建一个ProfiledDbCommand“在幕后”使用 DB2Command 类的对象。

于 2016-06-01T21:20:47.040 回答