2010-01-17 90 views
2

我試圖使用打開文件流運API

OpenSqlFilestream

指令,但我的代碼不承認它

我有什麼DLL才能使用它來加載?


親愛marc_s看看我的代碼

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() '.Connection.State = ConnectionState.Open Then RemoteSQLtx.Connection.Close() 
     '-------------------- Open 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 

回答

2

你做的所有打開文件流之前必要的步驟?抓取交易中的上下文?

查看這個article here的詳細討論如何做到這一點。這是另一個帶有VB.NET示例代碼片段的blog post


更新: Lefteris,您的代碼段是非常非常難以閱讀和理解,我做了我最好的,但我真的不知道。我在想的是你太快關閉了交易。您似乎創建了一個連接和一個事務來獲取GET_FILESTREAM_TRANSACTION_CONTEXT,但是在您實際寫出字節之前再次關閉它。我認爲事務應該跨越所有的操作 - 它應該在你獲得事務上下文之前開始,並且它應該在整個寫操作中保持活動狀態,並且只有在整個寫操作完成後纔會被提交。

我試圖想出一個簡單的代碼片段來告訴你 - 這是在C#,因爲我無法將其轉換回VB.NET:

public static void WriteFileStream(byte[] imSource, int imSize) 
{ 
    // use your own SQL insert command here instead 
    const string InsertCmd = "INSERT INTO PhotoAlbum(PhotoId, Description)" + 
      " VALUES(@PhotoId, @Description)"; 

    using (SqlConnection conn = new SqlConnection(ConnStr)) 
    { 
     conn.Open(); 

     // create transaction here and let it stay alive! 
     using (SqlTransaction txn = conn.BeginTransaction()) 
     { 
      using (SqlCommand cmd = new SqlCommand(InsertCmd, conn, txn)) 
      { 
       cmd.Parameters.Add("@PhotoId", SqlDbType.Int).Value = photoId; 
       cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = desc; 
       cmd.ExecuteNonQuery(); 
      } 

      SafeFileHandle handle = GetOutputFileHandle(photoId, txn); 

      MemoryStream inputStream = new MemoryStream(imSource); 

      using (FileStream dest = new FileStream(handle, FileAccess.Write)) 
      { 
       byte[] buffer = new byte[BlockSize]; 
       int bytesRead; 

       while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0) 
       { 
        dest.Write(buffer, 0, bytesRead); 
       } 
       dest.Close(); 
      } 

      inputStream.Close(); 

      // commit transaction here, after all is done     
      txn.Commit(); 
     } 
     conn.Close(); 
    } 
} 

這是從博客文章SQL Server 2008 FILESTREAM Part 3: OpenSqlFileStream API適應 - 一定要檢查一下!

+0

我在上面 但鋼的鏈接看到嗯,我已經做了所有的instractions已經返回錯誤「拒絕訪問」 現在看看我的下一個評論 – 2010-01-19 16:42:13

+0

我不得不改變我的命令代碼;打開文件流文件之前關閉所有打開的連接(在我的服務器和我的數據庫中的其他) 另外我關閉交易 我改變登錄帳戶在我的電腦,然後我重新打開所有上述連接 新的帳戶是從服務器計算機 ,現在我正在一個新的消息 「函數有錯誤的參數」 ,現在我的Active Directory中註冊一個Windows系統正帳戶再次堆疊 – 2010-01-19 16:47:26

+0

很難弄清楚什麼地方出了問題 - 除非你開始在你的原始問題中發佈一些代碼,以便我們可以看到什麼可能是錯的。 – 2010-01-19 16:49:19