Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 无法完成请求的操作,因为连接已断开_Linq - Fatal编程技术网

Linq 无法完成请求的操作,因为连接已断开

Linq 无法完成请求的操作,因为连接已断开,linq,Linq,此代码有什么问题,如何修复 我使用的是.NET3.5、SQLServer2008Express版本和Windows764。 我得到这个错误: [UpdateFileStatus]错误- System.InvalidOperationException:无法完成请求的操作,因为连接已断开。 位于System.Data.SqlClient.SqlInternalConnectionDS.ExecuteTransaction(TransactionRequest TransactionRequest,

此代码有什么问题,如何修复

我使用的是.NET3.5、SQLServer2008Express版本和Windows764。 我得到这个错误:

[UpdateFileStatus]错误-
System.InvalidOperationException:无法完成请求的操作,因为连接已断开。 位于System.Data.SqlClient.SqlInternalConnectionDS.ExecuteTransaction(TransactionRequest TransactionRequest,字符串名称,IsolationLevel iso,SqlInternalTransaction internalTransaction,布尔IsDeleteGateControlRequest) 在System.Data.SqlClient.SqlDelegatedTransaction.Initialize()处 在System.Transactions.TransactionStatePSPEOperation.PSPEInitialize(InternalTransaction tx,IPromotableSinglePhaseNotification promotableSinglePhaseNotification promotableSinglePhaseNotification) 在System.Transactions.TransactionStateActive.EnglistPromotableSinglePhase(内部事务tx、IPromotableSinglePhaseNotification promotableSinglePhaseNotification、事务原子事务) 在System.Transactions.Transaction.EnglistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification) 位于System.Data.SqlClient.SqlInternalConnection.EnclestNonNull(事务发送) 位于System.Data.SqlClient.SqlInternalConnection.Enlist(事务发送) 在System.Data.SqlClient.SqlInternalConnection.EnglistTransaction(事务事务处理)处 在System.Data.SqlClient.SqlConnection.EnglistTransaction(事务事务处理)处 位于System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser用户) 在System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe()处 位于System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode()处 位于System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式查询) 在System.Data.Linq.Table
1.System.Linq.IQueryProvider.Execute[TResult](表达式)中
位于System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
1源,表达式`1谓词) 位于StorageQuest.Ovm.Connection.Dal.FilesDal.GetFile(Guid文件ID、Int32文件FolderId、Int32文件FolderFileId、Int16文件版本) 位于StorageQuest.Ovm.Connection.Dal.FileDal.UpdateFileStatus(Guid文件ID、Int32文件FolderId、Int32文件FolderFileId、Int16文件版本、Guid discId、FileStatus文件状态) 在StorageQuest.Ovm.Connection.Bll.FileBll.UpdateFileStatus(Guid文件ID、Int32文件FolderId、Int32文件FolderFileId、Int16文件Resion、Guid discId、FileStatus文件状态)


提前感谢。

调用GetFile()尝试删除TransactionScope时引发错误。您不需要它,因为只有在
SaveFile
中,您正在写入数据库,并且那里只有一个
SubmitChanges
。您的意思是将GetFile()移出事务范围吗?不,删除它,使用语句删除
。我会这样做,然后等待客户的反馈。
        public bool UpdateFileStatus(Guid fileId, int fileFolderId, int fileFolderFileId, short fileVresion, Guid discId, FileStatus fileStatus)
    {
        FilesDal filesDAL = new FilesDal(this.connection);

        try
        {
            using (TransactionScope transcope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.Zero ))
            {
                bool result = filesDAL.UpdateFileStatus(fileId, fileFolderId, fileFolderFileId, fileVresion, discId, fileStatus);
                transcope.Complete();
                return result;
            }
        }
        catch (Exception ex)
        {
            SqlConn.LogConnection.Error(null, ex);
        }

        return false;
    }

        internal bool UpdateFileStatus(Guid fileId, int fileFolderId, int fileFolderFileId, short fileVersion, Guid discId, FileStatus fileStatus)
    {
        File file = null;

        try
        {
            file = GetFile(fileId, fileFolderId, fileFolderFileId, fileVersion);
        }
        catch (Exception ex)
        {
            SqlConn.LogConnection.Error(null, ex);
        }

        if (file == null)
            return false;

        file.FileDiscID = discId;
        file.FileStatus = (byte)fileStatus;

        try
        {
            return SaveFile(file);
        }
        catch (Exception ex)
        {
            SqlConn.LogConnection.Error(null, ex);
        }

        return false;
    }

        internal File GetFile(Guid fileId, int fileFolderId, int fileFolderFileId, short fileVersion)
    {
        using (FilesDataContext fileDC = new FilesDataContext(this.connection)
            {
                DeferredLoadingEnabled = false,
                CommandTimeout = 0
            })

            return fileDC.Files.FirstOrDefault(
                    f => f.FileID.Equals(fileId) &&
                         f.FileFolderID.Equals(fileFolderId) &&
                         f.FileFolderFileID.Equals(fileFolderFileId) &&
                         f.FileVersion.Equals(fileVersion)
                            );
    }

        internal bool SaveFile(File file)
    {
        ChangeSet changeSet = null;
        int changeCount = 0;

        using (FilesDataContext fileDC = new FilesDataContext(this.connection)
            {
                DeferredLoadingEnabled = false,
                CommandTimeout = 0
            })
        {
            if (file.FileRowVersion == null)  //insert a file
            {
                fileDC.Files.InsertOnSubmit(file);
                changeSet = fileDC.GetChangeSet();
                changeCount = changeSet.Inserts.Count;
            }
            else                              //updates a file
            {
                fileDC.Files.Attach(file, true);
                changeSet = fileDC.GetChangeSet();
                changeCount = changeSet.Updates.Count;
            }

            try
            {
                fileDC.SubmitChanges();
            }
            catch (ChangeConflictException cce)
            {
                fileDC.Log = Console.Out;
                Console.WriteLine(cce);
                SqlConn.LogConnection.Error(null, cce);
                //SqlConn.ChangeConflictException(fileDC);
                return false;
            }
            catch (InvalidOperationException ioe)
            {
                SqlConn.LogConnection.Error(null, ioe);
                return false;
            }
            catch (Exception ex)
            {
                SqlConn.LogConnection.Error(null, ex);
                return false;
            }
        }

        return !changeCount.Equals(0) ? true : false;
    }