Search code examples
sqlvb.netvisual-studio-2008visual-studio-2010filestream

ACCESS DENIED error from stream file open


I have create my code in order to write images in a remote sql server

All the details of accessing and writing are fine until now, including the system account right now i'm in the command of:

SqlFileStream = New SqlFileStream(filePathName, fileToken, FileAccess.Write)

and when i'm trying to execute it the Server returns the error 'Access denied' I have try all the posible ( those which i know) combinations to overcome this error but nothing Please give me the best assistance you may have

I've put a sniffer in my PC to lookup the packages between Server and my PC, so here what i got: the first addres is the Server address and the second is my PC address.

*"10.93.1.29","10.93.1.10","SMB","Tree Connect AndX Request, Path: \DEVELOPER\SQLEXPRESS "

"10.93.1.10","10.93.1.29","SMB","Tree Connect AndX Response"

"10.93.1.29","10.93.1.10","SMB","Trans2 Request, QUERY_PATH_INFO, Query File Basic Info, Path: \v1\RemoteDB\dbo\tPImages\tPImages_Image\DB6F1B11-2FAF-4326-8E44-FBA71DA94CEC\b8010a0f1aaf47c1888aab2e830dff43"

"10.93.1.10","10.93.1.29","SMB","Trans2 Response, QUERY_PATH_INFO, Error: STATUS_ACCESS_DENIED"

"10.93.1.29","10.93.1.10","SMB","NT Trans Request, NT CREATE"

"10.93.1.10","10.93.1.29","SMB","NT Trans Response, NT CREATE, FID: 0x0000, Error: STATUS_ACCESS_DENIED"*

I dare to say that this error comes from the Windows program when the SQL 2008 tries to write some DATA to the filies which creates on C:\sqlRemData..... (but finally i'm not sure even for that)

PLEASE if anyone knows?


Solution

  • Try this..

         public static bool SetAcl(string filename, string account) 
    { 
        FileSystemAccessRule rule = new FileSystemAccessRule(account, FileSystemRights.Write, AccessControlType.Allow); 
    
        PermissionSet fp = new PermissionSet(PermissionState.Unrestricted); 
        fp.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read, new string[] { filename })); 
        fp.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.PathDiscovery, new string[] { filename })); 
        fp.Assert(); 
    
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(filename)); 
    
    
        bool what = false; 
        DirectorySecurity security = di.GetAccessControl(); 
    
        security.ModifyAccessRule(AccessControlModification.Add, rule, out what); 
        di.SetAccessControl(security); 
        return what; 
    
    }