【问题标题】:Global temp table doesn't exist any more after calling stored procedure with this.ExecuteMethodCall in linq to sql在 linq to sql 中使用 this.ExecuteMethodCall 调用存储过程后,全局临时表不再存在
【发布时间】:2016-11-27 04:52:31
【问题描述】:

首先我创建了一个调用存储过程的临时表。

然后,在尝试从第二个存储过程从同一个 dbcontext 获取结果时,我收到一条错误消息,提示临时表不再存在。

这是不完整的代码。

    private void GetTempResult()
    {
        var tempTable = "##mytaemptable";
        //  var task = Task.Factory.StartNew(() =>  Services.StartPreparingTempList(clientId,tempTable));
        // execute stored procedure to create temp table 
        Services.StartPreparingTempList(clientId, tempTable); // temptable gets created successfully here .

        // execute stored procedure to get results from the above created temp table.
        var tempResults = Services.GetPartialTempList(tempTable, jtableArgs); //getting error here . As soon as this statement gets executed the temp table ceases to exist. 
    }

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetPartialTempEmailList")]
    public ISingleResult<JournalEmail> GetPartialTempEmailList([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="NVarChar(MAX)")] string tempTableName, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> startIndex, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> maxRowCount)
    {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), tempTableName, startIndex, maxRowCount);
            return ((ISingleResult<JournalEmail>)(result.ReturnValue));
    }

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.StartPreparingTempList")]
    public int StartPreparingTempList([global::System.Data.Linq.Mapping.ParameterAttribute(Name="ClientID", DbType="NVarChar(150)")] string clientID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="NVarChar(MAX)")] string tempTableName, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> maxRowCount)
    {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), clientID, tempTableName, maxRowCount);
            return ((int)(result.ReturnValue));
    }
}

是否使用以下语句在新的连接/会话中执行存储过程?一旦这个语句被执行,temptable 就不再存在并抛出错误:无效的对象名称。

IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), tempTableName, startIndex, maxRowCount);

有人可以指导我缺少什么吗?

【问题讨论】:

  • 为什么要在 linq-to-sql 中创建临时表?导入视图可能会更好?
  • 如果两个单独的会话同时执行会发生什么?不知道为什么它不适合你,但基本上是因为我会移动几座大山以避免首先这样做。问题可能出在存储过程本身...
  • @Arion,想将临时结果存储在某个地方..我想到了这一点。现在做一个概念验证以稍后获得部分结果可以改变存储。导入视图可能会更好没有太多想法..:)
  • @Tony,现在不需要担心会话 ..只是想看看功能正常工作。当我在从应用程序调用存储过程之前使用硬编码值执行存储过程时,它运行正常。
  • 有趣的是,如果你在不同的会话中执行这两个过程,我很难想出任何理由,为什么从代码中执行 SP 会消失你看到的任何东西。并不是说没有 :D 只是我什至无法猜测为什么现在会发生这种情况。

标签: c# .net sql-server-2008 linq-to-sql


【解决方案1】:

我猜你正在尝试创建一个全局临时表并在一个存储过程中插入值(我们将其命名为 sp1)并尝试在 sp2 中检索另一个值(我们将其命名为 sp2)。 在检索第二个存储过程 (sp2) 时,此问题发生在 linqtosql(in IExecuteResult result) 中。 我修复了这个问题,但是将 dbml 文件中的存储过程 (sp2) 的返回类型更改为一个类,其中列名作为我在 dbml 设计器中创建的属性类型 或者 您可以创建一个与全局临时表具有相同返回类型的视图或表,并将其添加到设计器中。然后,您可以使用视图/表作为存储过程 (sp2) 的返回类型

在图片插图中 sp_EditMVC_reconcile_fix_get_no_medical 是 (sp2) 我用来检索全局临时表的 reconcile_no_medical_temp 是我创建的类。

解决方案 2:

我认为这是VS中的一个错误,所以我找不到其中的逻辑,我现在没有时间找到。

在 SP1 中添加 SP2 查询并使用自定义创建的类作为返回类型。然后 SP2 将工作。不知道原因,但如果上述解决方案失败,它将修复它。 对不起,我的英语不好。 希望对你有帮助

【讨论】:

    猜你喜欢
    • 2013-11-01
    • 2011-07-26
    • 1970-01-01
    • 2015-12-11
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多