【问题标题】:How to get the parameter declarations as well as the SQL calls of NHibernate?如何获取参数声明以及 NHibernate 的 SQL 调用?
【发布时间】:2010-08-04 08:35:04
【问题描述】:

当我使用 NHibernate 时(顺便说一句,我是它的初学者),我能够在我的输出中获得对 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 中执行此操作时,出现错误:

消息 137,第 15 级,状态 2,第 1 行 必须声明标量变量“@p0”。

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

谢谢

【问题讨论】:

    标签: c# .net sql nhibernate


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      这样的东西应该可以工作(未经测试,我没有你的数据库):

      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);
      

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 2011-02-13
        • 2021-08-03
        • 2019-12-12
        • 2021-03-04
        • 1970-01-01
        • 1970-01-01
        • 2013-12-05
        • 2015-02-08
        相关资源
        最近更新 更多