Troubleshooting Guide: SQL Server Error 5541 – FILESTREAM Open Mode Violation
Why This Error Happens
Error 5541 occurs when an application attempts to open a FILESTREAM data column using the Win32 file handle (via OpenSqlFilestream or SqlFileStream) without specifying a valid access mode, such as read, write, or read/write. This typically happens because the calling application passes an invalid or null AccessMode parameter, or because the transaction context attached to the FILESTREAM handle has already been committed, rolled back, or closed before the file operation executed. It can also surface when the NTFS permissions on the FILESTREAM data container conflict with the access mode requested by the client driver.
Step-by-Step Resolution Guide
Step 1: Verify FILESTREAM is Properly Enabled at the Instance Level
- Open SQL Server Configuration Manager on Windows Server 2025/Windows 11.
- Navigate to SQL Server Services → right-click your instance → Properties → FILESTREAM tab.
- Confirm “Enable FILESTREAM for Transact-SQL access” and “Enable FILESTREAM for file I/O streaming access” are both checked.
- Run the following T-SQL to confirm instance-level configuration matches:
EXEC sp_configure filestream_access_level;
RECONFIGURE;
Step 2: Review and Correct the Application Access Mode Parameter
- Inspect the client code calling
SqlFileStream(ADO.NET) orOpenSqlFilestream(native client). - Ensure the access mode argument explicitly matches one of:
System.IO.FileAccess.Read,FileAccess.Write, orFileAccess.ReadWrite— nevernullor0. - Confirm the mode requested aligns with the transaction isolation level; a read-only transaction cannot request
Writeaccess. - Example corrected C# snippet:
SqlFileStream sfs = new SqlFileStream(path, transactionContext, FileAccess.ReadWrite);
Step 3: Validate Transaction Scope and Handle Lifetime
- Ensure the FILESTREAM handle is opened within an active transaction and not after
COMMITorROLLBACKhas executed. - Confirm the transaction context byte array (
GET_FILESTREAM_TRANSACTION_CONTEXT()) is retrieved immediately before the file open call. - Avoid reusing a transaction context across multiple threads or after the originating
SqlTransactionobject has been disposed.
Step 4: Check NTFS Permissions on the FILESTREAM Container
- Navigate to the FILESTREAM data folder (e.g.,
D:\SQLData\FileStreamContainer\). - Right-click → Properties → Security tab.
- Confirm the SQL Server service account has Modify and Read/Execute permissions, not just Read.
- Apply changes and restart the SQL Server service if permissions were modified.
Alternative or Verification Steps
- Re-run the failing operation using
FileAccess.ReadWriteexplicitly, then querysys.dm_tran_active_transactionsto confirm the transaction remains open during the file I/O call. - If the error persists, bypass the Win32 handle approach entirely and use T-SQL
UPDATE/INSERTwith.WRITE()on the FILESTREAM column as a temporary workaround while debugging the client code.