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、ブール isDelegateControlRequest) で System.Data.SqlClient.SqlDelegatedTransaction.Initialize() で System.Transactions.TransactionStatePSPEOperation.PSPEInitialize (InternalTransaction tx、 System.Transactions.TransactionStateActive.EnlistPromotableSinglePhase の IPromotableSinglePhaseNotification promotableSinglePhaseNotification) (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.GetFile(Guid fileId, Int32 fileFolderId, Int32 fileFolderFileId, Int16 fileVersion) で 1 ソース、Expression`1 述語) StorageQuest.Ovm.Connection.Dal.FilesDal.UpdateFileStatus(Guid fileId, Int32) でFileFolderId、Int32 fileFolderFileId、Int16 fileVersion、Guid discId、FileStatus fileStatus) (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