0

这段代码有什么问题,我该如何解决?

我正在使用 .net 3.5、Sql Server 2008 Express 版本和 Windows 7 64。我收到此错误:

[UpdateFileStatus] 错误 -
System.InvalidOperationException:由于连接中断,请求的操作无法完成。在 System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, 字符串名称, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest) 在 System.Data.SqlClient.SqlDelegatedTransaction.Initialize() 在 System.Transactions.TransactionStatePSPEOperation.PSPEInitialize(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification) 在 System.Transactions.TransactionStateActive.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction) 在 System.Transactions.Transaction。1.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryableStorageQuest.Ovm.Connection.Dal.FilesDal.UpdateFileStatus(Guid fileId, Int32) 处的 1 个源,表达式`1 谓词) FileFolderId, Int32 fileFolderFileId, Int16 fileVersion, Guid discId, FileStatus fileStatus) at StorageQuest.Ovm.Connection.Bll.FilesBll.UpdateFileStatus(Guid fileId, Int32 fileFolderId, Int32 fileFolderFileId, Int16 fileVresion, Guid discId, FileStatus fileStatus)

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

先感谢您。

4

0 回答 0