【发布时间】: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