如何获取参数声明以及NHibernate的SQL调用?

时间:2010-08-04 08:35:04

标签: c# .net sql nhibernate

当我使用NHibernate时(我是btw的初学者)我能够在输出中获得对Db的SQL调用。但是在复制并粘贴到Management Studio后,我无法使它们正常工作。

因为缺少参数声明。

我得到类似的东西:

SELECT this_.PK_Product as PK1_1_0_, this_.ProductCode as ProductC2_1_0_
, this_.ProductName as ProductN3_1_0_, this_.ProductCodeISO2 as ProductC4_1_0_
, this_.NotUsed as NotUsed1_0_, this_.Confirmed as Confirmed1_0_, this_.LabelRequired as LabelReq7_1_0_ FROM tProduct this_ 
WHERE (this_.NotUsed = @p0 and this_.Confirmed = @p1 and this_.LabelRequired = @p2);@p0 = False [Type: Boolean (0)], @p1 = True [Type: Boolean (0)], @p2 = False [Type: Boolean (0)]`enter code here`

当我在SQL Management Studio中执行此操作时,出现错误:

Msg 137,Level 15,State 2,Line 1 必须声明标量变量“@ p0”。

我可以告诉NHibernate在创建的查询字符串中添加参数声明吗?

由于

2 个答案:

答案 0 :(得分:3)

对于分析我正在使用Microsoft SQL Server Management Studio或NHibernate Profiler附带的SQL Server Profiler。

在SQL Server Profiler中,您可以看到语句以及参数声明和值,因此您只需将它们复制并粘贴到管理工作室即可。

在NHibernate探查器中,您还可以看到SQL语句,但参数已经被值替换(您可以在注释中看到参数名称)。我强烈推荐NHibernate profiler,你可以下载试用版。

答案 1 :(得分:-1)

这样的事情应该有用(没有经过测试,我没有你的数据库):

DECLARE @p0 BIT
DECLARE @p1 BIT
DECLARE @p2 BIT

SET @p0 = 0
SET @p1 = 1
SET @p2 = 0

SELECT
 this_.PK_Product as PK1_1_0_,
 this_.ProductCode as ProductC2_1_0_,
 this_.ProductName as ProductN3_1_0_,
 this_.ProductCodeISO2 as ProductC4_1_0_,
 this_.NotUsed as NotUsed1_0_,
 this_.Confirmed as Confirmed1_0_,
 this_.LabelRequired as LabelReq7_1_0_
 FROM tProduct this_ 
WHERE
 (this_.NotUsed = @p0 and this_.Confirmed = @p1 and this_.LabelRequired = @p2);