前端使用VB后端使用Sql2000数据库,如何把Word文件,是word文件不是文件名。保存在Sql2000数据库中?

解决方案 »

  1.   

    使用流对象保存和显示图片 
    打开vb6,新建工程。添加两个按钮,一个image控件
    注意:Access中的photo字段类型为OLE对象.
    SqlServer中的photo字段类型为Image'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
    ‘2.5版本以下不支持Stream对象
    Dim iConcstr As String
    Dim iConc As ADODB.Connection
     '保存文件到数据库中
    Sub s_SaveFile()
        Dim iStm As ADODB.Stream
        Dim iRe As ADODB.Recordset
        Dim iConcstr As String    '读取文件到内容
        Set iStm = New ADODB.Stream
        With iStm
            .Type = adTypeBinary   '二进制模式
            .Open
            .LoadFromFile App.Path + "\test.jpg"
        End With
           '打开保存文件的表
        Set iRe = New ADODB.Recordset
        With iRe
            .Open "select * from img", iConc, 1, 3
            .AddNew         '新增一条记录
            .Fields("photo") = iStm.Read
            .Update
        End With
          '完成后关闭对象
        iRe.Close
        iStm.Close
    End Sub
    Sub s_ReadFile()
        Dim iStm As ADODB.Stream
        Dim iRe As ADODB.Recordset
        '打开表
    Set iRe = New ADODB.Recordset
    ‘得到最新添加的纪录
        iRe.Open "select top 1 * from img order by id desc", iConc, adOpenKeyset, adLockReadOnly
        '保存到文件
        Set iStm = New ADODB.Stream
        With iStm
            .Mode = adModeReadWrite
            .Type = adTypeBinary
            .Open
            .Write iRe("photo")
    ‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
            .SaveToFile App.Path & "\test1.jpg"
        End With
           Image1.Picture = LoadPicture(App.Path & "\test1.jpg")
       '关闭对象
        iRe.Close
        iStm.Close
    End Sub
     Private Sub Command1_Click()
    Call s_ReadFile
    End Sub
    Private Sub Command2_Click()
    Call s_SaveFile
    End Sub
    Private Sub Form_Load()
        '数据库连接字符串
        iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
            ";Data Source=F:\csdn_vb\database\保存图片\access图片\img.mdb"‘下面的语句是连接sqlserver数据库的.
        ‘iConcstr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
    ‘ "User ID=sa;Password=;Initial Catalog=test;Data Source=yang"
        Set iConc = New ADODB.Connection
       iConc.Open iConcstr
    End Sub
     Private Sub Form_Unload(Cancel As Integer)
    iConc.Close
    Set iConc = Nothing
    End Sub
      

  2.   

    re: Leftie(左手,为人民币服务) 您说的是保存图像,我说的是Word文件,做法是一样的吗?
      

  3.   

    图像文件是作为二进制文件存储的而word文件也是如此
      

  4.   

    往数据库(SQL/Acess)读写Word文件:
    '窗体放一个CommonDialg1,一个Command1(保存Word),一个Command2(读出Word)
    '引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
    '字段myWord为binary类型(SQL库)或Ole对象类型(Access库)
    Private Sub Command1_Click()
      On Error GoTo err
      Dim StmPic As ADODB.Stream
        '保存你所选择的文件
        Set StmPic = New ADODB.Stream
        StmPic.Type = adTypeBinary    '指定流是二进制类型
        CommonDialog1.ShowOpen    
        StmPic.Open                     '将数据获取到Stream对象中
        StmPic.LoadFromFile (CommonDialog1.FileName) '将选择的文件加载到打开的StmPic中
        rs.AddNew
        rs.Fields("myWord").Value = StmPic.Read   '从StmPic对象中读取数据
        rs.Update
        StmPic.Close
      Exit Sub
    err:
      MsgBox err.Description
    End SubPrivate Sub Command2_Click()
         Dim StmPic As ADODB.Stream
         On Error GoTo err
            StrPicTemp="c:\temp.doc"
            Set StmPic = New ADODB.Stream
            With StmPic
                .Type = adTypeBinary
                .Open
                .Write rs.Fields("myWord")       '写入数据库中的数据至Stream中
                .SaveToFile StrPicTemp, adSaveCreateOverWrite  '将Stream中数据写入临时文件中(C:\temp.doc)
                .Close
            End With
            Exit Sub
    err:
         MsgBox err.Description
    End Sub
      

  5.   

    除了以上各位兄弟提出的Stream对象外,还可以
    使用ADO Field对象的GetChunk, AppendChunk来读取和写入二进制数据,安装MSDN后
    ms-help://MS.MSDNQTR.2005JUL.1033/ado270/htm/mdmthappendchunkx.htmAppendChunk and GetChunk Methods Example (VB)
    This example uses the AppendChunk and GetChunk methods to fill an image field with data from another record.'BeginAppendChunkVB    'To integrate this code
        'replace the data source and initial catalog values
        'in the connection string
        
    Public Sub Main()
        On Error GoTo ErrorHandler    'recordset and connection variables
        Dim Cnxn As ADODB.Connection
        Dim strCnxn As String
        Dim rstPubInfo As ADODB.Recordset
        Dim strSQLPubInfo As String
         'record variables
        Dim strPubID As String
        Dim strPRInfo As String
        Dim lngOffset As Long
        Dim lngLogoSize As Long
        Dim varLogo As Variant
        Dim varChunk As Variant
        Dim strMsg As String
        
        Const conChunkSize = 100
        
        ' Open a connection
        Set Cnxn = New ADODB.Connection
        strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _
            "Initial Catalog='Pubs';Integrated Security='SSPI';"
        Cnxn.Open strCnxn
        
        ' Open the pub_info table with a cursor that allows updates
        Set rstPubInfo = New ADODB.Recordset
        strSQLPubInfo = "pub_info"
        rstPubInfo.Open strSQLPubInfo, Cnxn, adOpenKeyset, adLockOptimistic, adCmdTable
        
        ' Prompt for a logo to copy
        strMsg = "Available logos are : " & vbCr & vbCr
        Do While Not rstPubInfo.EOF
            strMsg = strMsg & rstPubInfo!pub_id & vbCr & _
                Left(rstPubInfo!pr_info, InStr(rstPubInfo!pr_info, ",") - 1) & _
                vbCr & vbCr
            rstPubInfo.MoveNext
        Loop
       
        strMsg = strMsg & "Enter the ID of a logo to copy:"
        strPubID = InputBox(strMsg)
        
        ' Copy the logo to a variable in chunks
        rstPubInfo.Filter = "pub_id = '" & strPubID & "'"
        lngLogoSize = rstPubInfo!logo.ActualSize
        Do While lngOffset < lngLogoSize
            varChunk = rstPubInfo!logo.GetChunk(conChunkSize)
            varLogo = varLogo & varChunk
            lngOffset = lngOffset + conChunkSize
        Loop
       
        ' Get data from the user
        strPubID = Trim(InputBox("Enter a new pub ID" & _
                                 " [must be > 9899 & < 9999]:"))
                                 
        strPRInfo = Trim(InputBox("Enter descriptive text:"))
        
        ' Add the new publisher to the publishers table to avoid
        ' getting an error due to foreign key constraint
        Cnxn.Execute "INSERT publishers(pub_id, pub_name) VALUES('" & _
                       strPubID & "','Your Test Publisher')"
        
        ' Add a new record, copying the logo in chunks
        rstPubInfo.AddNew
        rstPubInfo!pub_id = strPubID
        rstPubInfo!pr_info = strPRInfo    lngOffset = 0 ' Reset offset
        Do While lngOffset < lngLogoSize
            varChunk = LeftB(RightB(varLogo, lngLogoSize - lngOffset), _
                conChunkSize)
            rstPubInfo!logo.AppendChunk varChunk
            lngOffset = lngOffset + conChunkSize
        Loop
        rstPubInfo.Update
       
        ' Show the newly added data
        MsgBox "New record: " & rstPubInfo!pub_id & vbCr & _
            "Description: " & rstPubInfo!pr_info & vbCr & _
            "Logo size: " & rstPubInfo!logo.ActualSize    ' Delete new records because this is a demo
        rstPubInfo.Requery
        Cnxn.Execute "DELETE FROM pub_info " & _
            "WHERE pub_id = '" & strPubID & "'"    Cnxn.Execute "DELETE FROM publishers " & _
            "WHERE pub_id = '" & strPubID & "'"    ' clean up
        rstPubInfo.Close
        Cnxn.Close
        Set rstPubInfo = Nothing
        Set Cnxn = Nothing
        Exit Sub
        
    ErrorHandler:
       ' clean up
        If Not rstPubInfo Is Nothing Then
            If rstPubInfo.State = adStateOpen Then rstPubInfo.Close
        End If
        Set rstPubInfo = Nothing
        
        If Not Cnxn Is Nothing Then
            If Cnxn.State = adStateOpen Then Cnxn.Close
        End If
        Set Cnxn = Nothing
        
        If Err <> 0 Then
            MsgBox Err.Source & "-->" & Err.Description, , "Error"
        End If
    End Sub
    'EndAppendChunkVB