这段代码有什么问题,我该如何解决?
我使用的是.net 3.5,Sql Server 2008 Express版本是Windows 7 64。 我收到了这个错误:
[UpdateFileStatus]错误 -
System.InvalidOperationException:无法完成请求的操作,因为连接已断开。
at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest,String name,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.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification)
在System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
在System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx)
在System.Data.SqlClient.SqlInternalConnection.EnlistTransaction(事务事务)
在System.Data.SqlClient.SqlConnection.EnlistTransaction(事务事务)
在System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser用户)
在System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe()
在System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode()
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
在System.Data.Linq.Table 1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
1源,表达式`1谓词)
在StorageQuest.Ovm.Connection.Dal.FilesDal.GetFile(Guid fileId,Int32 fileFolderId,Int32 fileFolderFileId,Int16 fileVersion)
在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;
}
提前谢谢。