如何使用 VB6 将文件上传到 Oracle BLOB 字段?

发布于 2024-07-19 17:37:07 字数 60 浏览 1 评论 0原文

我想使用 VB6 从磁盘获取一个文件并将其上传到 Oracle BLOB 字段。 我怎样才能做到这一点?

I want to take a file from disk and upload it into an Oracle BLOB field, using VB6. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下慵懒的猫 2024-07-26 17:37:07

回答我自己的问题,供参考:

Public Function SaveFileAsBlob(fullFileName As String, documentDescription As String) As Boolean

    'Upload a binary file into the database as a BLOB
    'Based on this example: http://www.codeguru.com/forum/printthread.php?t=337027

    Dim rstUpload As ADODB.Recordset
    Dim pkValue AS Long
    On Error GoTo ErrorHandler

    Screen.MousePointer = vbHourglass        

    'Create a new record (but leave document blank- we will update the doc in a moment)
    'the where clause ensures *no* result set; we only want the structure
    strSQL = "SELECT DOC_NUMBER, DOC_DESC, BLOB_FIELD " & _
      " FROM MY_TABLE " & _
      " WHERE PRIMARY_KEY = 0" 
    pkValue = GetNextPKValue

    Set rstUpload = New ADODB.Recordset
    With rstUpload
      .CursorType = adOpenKeyset
      .LockType = adLockOptimistic
      .Open strSQL, myConn
      .AddNew Array("DOC_NUMBER", "DOC_DESC"), _
              Array(pkValue, documentDescription)
      .Close
    End With

    'They may have the document open in an external application.  Create a copy and work with that copy
    Dim tmpFileName As String
    tmpFileName = GetTempPath & ExtractFileName(fullFileName)
    'if the tmp file exists, delete it
    If Len(Dir(tmpFileName)) > 0 Then
      Kill tmpFileName
    End If

    'see this URL for info about this subroutine:
    'http://stackoverflow.com/questions/848087/how-can-i-copy-an-open-file-using-vb6
    CopyFileEvenIfOpen fullFileName, tmpFileName

    'Now that our record is inserted, update it with the file from disk
    Set rstUpload = Nothing
    Set rstUpload = New ADODB.Recordset
    Dim st As ADODB.Stream
    rstUpload.Open "SELECT BLOB_FIELD FROM MY_TABLE WHERE PRIMARY_KEY = " & pkValue
      , myConn, adOpenDynamic, adLockOptimistic
    Set st = New ADODB.Stream
    st.Type = adTypeBinary
    st.Open
    st.LoadFromFile (tmpFileName)
    rstUpload.Fields("BLOB_FIELD").Value = st.Read
    rstUpload.Update

    'Now delete the temp file we created
    Kill (tmpFileName)

    DocAdd = True
ExitPoint:
    On Error Resume Next
    rstUpload.Close
    st.Close
    Set rstUpload = Nothing
    Set st = Nothing
    Screen.MousePointer = vbDefault
    Exit Function
ErrorHandler:
    DocAdd = False
    Screen.MousePointer = vbDefault
    MsgBox "Source: " & Err.Source & vbCrLf & "Number: " & Err.Number & vbCrLf & Err.Description, vbCritical, _
         "DocAdd Error"
    Resume ExitPoint
End Function

Answering my own question, for reference:

Public Function SaveFileAsBlob(fullFileName As String, documentDescription As String) As Boolean

    'Upload a binary file into the database as a BLOB
    'Based on this example: http://www.codeguru.com/forum/printthread.php?t=337027

    Dim rstUpload As ADODB.Recordset
    Dim pkValue AS Long
    On Error GoTo ErrorHandler

    Screen.MousePointer = vbHourglass        

    'Create a new record (but leave document blank- we will update the doc in a moment)
    'the where clause ensures *no* result set; we only want the structure
    strSQL = "SELECT DOC_NUMBER, DOC_DESC, BLOB_FIELD " & _
      " FROM MY_TABLE " & _
      " WHERE PRIMARY_KEY = 0" 
    pkValue = GetNextPKValue

    Set rstUpload = New ADODB.Recordset
    With rstUpload
      .CursorType = adOpenKeyset
      .LockType = adLockOptimistic
      .Open strSQL, myConn
      .AddNew Array("DOC_NUMBER", "DOC_DESC"), _
              Array(pkValue, documentDescription)
      .Close
    End With

    'They may have the document open in an external application.  Create a copy and work with that copy
    Dim tmpFileName As String
    tmpFileName = GetTempPath & ExtractFileName(fullFileName)
    'if the tmp file exists, delete it
    If Len(Dir(tmpFileName)) > 0 Then
      Kill tmpFileName
    End If

    'see this URL for info about this subroutine:
    'http://stackoverflow.com/questions/848087/how-can-i-copy-an-open-file-using-vb6
    CopyFileEvenIfOpen fullFileName, tmpFileName

    'Now that our record is inserted, update it with the file from disk
    Set rstUpload = Nothing
    Set rstUpload = New ADODB.Recordset
    Dim st As ADODB.Stream
    rstUpload.Open "SELECT BLOB_FIELD FROM MY_TABLE WHERE PRIMARY_KEY = " & pkValue
      , myConn, adOpenDynamic, adLockOptimistic
    Set st = New ADODB.Stream
    st.Type = adTypeBinary
    st.Open
    st.LoadFromFile (tmpFileName)
    rstUpload.Fields("BLOB_FIELD").Value = st.Read
    rstUpload.Update

    'Now delete the temp file we created
    Kill (tmpFileName)

    DocAdd = True
ExitPoint:
    On Error Resume Next
    rstUpload.Close
    st.Close
    Set rstUpload = Nothing
    Set st = Nothing
    Screen.MousePointer = vbDefault
    Exit Function
ErrorHandler:
    DocAdd = False
    Screen.MousePointer = vbDefault
    MsgBox "Source: " & Err.Source & vbCrLf & "Number: " & Err.Number & vbCrLf & Err.Description, vbCritical, _
         "DocAdd Error"
    Resume ExitPoint
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文