打开流文件中的ACCESS DENIED错误

时间:2010-01-17 13:20:37

标签: sql vb.net visual-studio-2008 visual-studio-2010 filestream

我已经创建了我的代码,以便在远程sql server中写入图像

到目前为止,访问和写入的所有细节都很好,包括现在的系统帐户我在以下命令:

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

当我试图执行它时,服务器返回错误'拒绝访问'我已尝试所有可能的(我知道的)组合来克服此错误但没有什么请给我最好的帮助你可能有< / p>

我在我的电脑上放了一个嗅探器来查找服务器和我的电脑之间的软件包,所以我得到的是:第一个地址是服务器地址,第二个是我的电脑地址。

*“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请求,QUERY_PATH_INFO,查询文件基本信息,路径:\ v1 \ RemoteDB \ dbo \ tPImages \ tPImages_Image \ DB6F1B11-2FAF-4326-8E44 -FBA71DA9​​4CEC \ b8010a0f1aaf47c1888aab2e830dff43"

“10.93.1.10”,“10.93.1.29”,“SMB”,“Trans2响应,QUERY_PATH_INFO,错误: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,错误:STATUS_ACCESS_DENIED”*

我敢说这个错误来自Windows程序,当SQL 2008试图将一些DATA写入在C:\ sqlRemData上创建的文件时.....(但最后我甚至不确定)

如果有人知道吗?

2 个答案:

答案 0 :(得分:1)

试试这个..

     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; 

} 

答案 1 :(得分:0)

    Public Sub WriteFileStream(ByVal imSource As Byte(), ByVal imSize As Integer, ByVal imTy As Type, ByVal sender As Object, ByVal e As EventArgs)
    Dim subProvider As String = Nothing
    Dim subDataSource As Object = Nothing
    Dim subUid As String = Nothing
    Dim subPwd As String = Nothing
    Dim subDataBase As String = Nothing
    Dim subPSI As Boolean = Nothing
    Dim ParamXML() As String = Nothing
    Dim TypeOfServer As String = "Remote"
    Dim imParam(3) As String
    Dim imTable(1) As String
    Dim RemoteSQLcmd As SqlCommand = New SqlCommand
    Dim tokenReader As SqlDataReader
    '-------------------------------------------------'

    Dim AbsRecord As Int64 = 0
    Dim VarString(10) As String
    Dim VarInt(10) As Integer
    '-------------------------------------------------'
    ParamXML = Split(loadXmlFile(TypeOfServer, sender, e), "|")
    subUid = ParamXML(3)
    subProvider = ParamXML(0)
    subDataSource = ParamXML(1)
    subDataBase = ParamXML(2)
    subPwd = ParamXML(4)
    subPSI = ParamXML(5)

    Dim SchemaID As String = Convert.ToInt16(ParamXML(8))
    Dim SchemaName As String = Nothing
    If SchemaID = 1 Then
        SchemaName = subUid
    ElseIf SchemaID = 2 Then
        SchemaName = "dbo"
    ElseIf SchemaID = 0 Then
        SchemaName = "dbo"
    End If
    '-------------------------------------------------'
    imTable(0) = "tPDetails"
    imTable(1) = "tPImages"
    Try
        imParam(0) = Me.TextBox1.Text.Trim.ToString     'Name'
        imParam(1) = Me.TextBox2.Text.Trim.ToString     'Code'
        imParam(2) = Me.TextBox3.Text.Trim.ToString     'Price'
        imParam(3) = Me.TextBox4.Text.Trim.ToString     'Comments'
        '========================================================'
    If RemoteSQLConn.State = ConnectionState.Open Then RemoteSQLConn.Close()
    SQL_Connection(TypeOfServer, TypeOfServer & "Conn.xml", sender, e)
    RemoteSQLConn.open()

        '----------------------'
        Dim imHolder As Image = Image.FromStream(imStream)
        Dim imHeight As Integer = imHolder.Height
        Dim imWidth As Integer = imHolder.Width
        Dim imLength As Integer = imHolder.PropertyItems.Length
        Dim imType As Type = imTy
        '----------------------'
        Dim FirstColumnNames As String = _
                                imTable(0) & "_Code, " & _
                                imTable(0) & "_Price, " & _
                                imTable(0) & "_Title, " & _
                                imTable(0) & "_Type, " & _
                                imTable(0) & "_Height, " & _
                                imTable(0) & "_Width, " & _
                                imTable(0) & "_Stock, " & _
                                imTable(0) & "_Comments "
        Dim FirstFieldsValues As String = "'" & imParam(1) & "', '" & _
                                imParam(2) & "', '" & _
                                imParam(0) & "', '" & _
                                imType.ToString & "', '" & _
                                imHeight & "', '" & _
                                imWidth & "', '" & _
                                "0', '" & _
                                imParam(3) & "' "
'--------------------------------------------'
           RemoteSQLcmd = New SqlCommand("INSERT INTO " & _
    SchemaName & "." & imTable(0) & " (" & FirstColumnNames & ")                     VALUES (" & FirstFieldsValues & ") ", RemoteSQLConn)
            RemoteSQLcmd.ExecuteNonQuery()
            '--------------------------------------------------'
    RemoteSQLcmd = New SqlCommand("SELECT * FROM " & SchemaName & "." & imTable(0) & _
    " WHERE " & imTable(0) & "_Code = " & "'" & imParam(1) & "'", RemoteSQLConn)
            AbsRecord = RemoteSQLcmd.ExecuteScalar
            '--------------------------------------------------'
      RemoteSQLcmd = New SqlCommand("INSERT INTO " & SchemaName & "." & imTable(1) & " VALUES (newid(), " & AbsRecord & ", CAST('' as varbinary(max)))", RemoteSQLConn)

            RemoteSQLcmd.ExecuteNonQuery()
            '--------------------------------------------------'
    RemoteSQLcmd = New SqlCommand("SELECT " & imTable(1) & "_Image.PathName() FROM " & _
SchemaName & "." & imTable(1) & " WHERE " & imTable(1) & "_" & imTable(0) & "_ID = " & AbsRecord, RemoteSQLConn)
            Dim filePathName As String = Nothing
            Dim pathObj As Object = RemoteSQLcmd.ExecuteScalar()
'--------------------------------------------------   Path Name  '
            If Not pathObj.Equals(DBNull.Value) Then
                filePathName = DirectCast(pathObj, String)
            Else
    Throw New System.Exception("Image.PathName() failed to read the path name for the Image column.")
            End If
    '--------------------------   GET_FILESTREAM_TRANSACTION_CONTEXT()'
            Dim RemoteSQLtx As SqlTransaction = RemoteSQLConn.BeginTransaction("MainTranaction")
            RemoteSQLcmd.Transaction = RemoteSQLtx
    RemoteSQLcmd = New SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", RemoteSQLConn, RemoteSQLtx)
            Dim tokenObject As Object = RemoteSQLcmd.ExecuteScalar()
        '--------------------------------------   File Token '
            tokenReader = RemoteSQLcmd.ExecuteReader(CommandBehavior.SingleRow)
            tokenReader.Read()
            Dim txContext As SqlBinary = DirectCast(tokenObject, Byte())
            tokenReader.Close()
            '-----------------------------------------------'
            Try
                '--------------------  Closing all connections'
       If RemoteSQLConn.State = ConnectionState.Open Then RemoteSQLConn.Close()
       If RemoteSQLcmd.Connection.State = ConnectionState.Open Then RemoteSQLcmd.Connection.Close()
                If RemoteConnInfo.State = ConnectionState.Open Then RemoteConnInfo.Close()
                RemoteSQLtx.Dispose() 
       '---------- Opening all connections for Indegrated Security'
                ChangeLoginPerson("PRINCIDEVEL\Administrator")
                RemoteConnInfo.ConnectionString = "Provider=" & subProvider & "; Data Source=" & subDataSource & _
                "; Database=" & subDataBase & "; Integrated Security=" & "SSPI" & "; Persist Security Info=" & subPSI
                RemoteSQLcmd.Connection.Open()
                RemoteSQLConn = New SqlConnection("Server=" & subDataSource & "; Integrated Security=TRUE" & "; database=" & subDataBase)
                RemoteSQLConn.Open()
                RemoteSQLtx = RemoteSQLConn.BeginTransaction("MainTranaction")
                RemoteSQLcmd.Transaction = RemoteSQLtx
                '-------------------------  Write in to file stream   ---------------'
                Dim imImage As Byte() = New Byte(imStream.Length) {}
                Dim bytesRead As Integer = imStream.Read(imImage, 0, imStream.Length)
                Dim sqlFile As New SqlFileStream(filePathName, txContext, FileAccess.ReadWrite, FileOptions.WriteThrough, 0)
                Dim numBytes As Integer = 0
                Dim unicode As Encoding = Encoding.GetEncoding(0)
                While bytesRead > 0
                    sqlFile.Write(imImage, 0, bytesRead)
                    bytesRead = imStream.Read(imImage, 0, imSize)
                End While
                RemoteSQLtx.Commit()
                RemoteSQLcmd.Transaction.Commit()
                sqlFile.Close()
            Catch FsEx As Exception
                MessageBox.Show(FsEx.Message, "Write in SQL File Stream ", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Finally
            End Try
            '--------------------------------------------------'
        Catch ex As Exception
MessageBox.Show(ex.Message, "WriteFileStream ", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Finalize()
        Finally
            RemoteSQLConn.Close()
            imStream.Close()
        End Try


    End Sub