sql如何插入图片?
我现在想用sql语句插入一些图片,前台是VB,后台是SQL.这些图片有可能是*,bmp,也有可能是*.ico,但是我现在想知道如何插入图片?我是这样做的,先在VB用commondialog,做一个询问路径,然后在用Sql语句插入,不知道这样行不行?具体语句如下:
Private Sub cmd_open_Click()
On Error Resume Next
 '打开文件夹,选择图片按钮   
    Dim pathname As String
    cd.Filter = "*.jpg|*.JPG|*.bmp|*.BMP|*.ico|*.ico"
    cd.ShowSave
    If Err.Number <> 0 Then
       ShowSave = False
    End If
    pathname = cd.FileName
    Text1.Text = pathname
End Sub
****************************************************
Private Sub cmd_add_Click()
'添加按钮
 Dim insertstr As String
    insertstr = "insert into T_picid (F_image) " & _
                "values('" & pathname & "')" & _
                "where F_id='" & Grid.TextMatrix(Grid.Row, 2) & "'"
    cn.Execute insertstr
End Sub

解决方案 »

  1.   

    Set sm = New ADODB.Stream     ‘向数据库里直接插入图片
    Set rs = New ADODB.Recordset
    Set conn = New ADODB.Connection
    conn.Open ConnectString
    sm.Mode = adModeReadWrite
    sm.Type = adTypeBinary
    sm.Open
    If Right(App.Path, 1) = "\" Then
      sm.LoadFromFile App.Path & "图标 \ FULL.ico"
    Else
      sm.LoadFromFile App.Path & "\" & "图标\full.ico"
    End If
    rs.Open Sql, conn, adOpenDynamic, adLockOptimistic
    rs.Update
    rs.Fields.Item(0).Value = sm.Read()
    rs.Update
    rs.Close
    sm.Close
    conn.Close
    Set conn_Two = New ADODB.Connection  ’从数据库里面读图片
    Set rs_two = New ADODB.Recordset
    Set sm = New ADODB.Stream
            sm.Mode = adModeReadWrite
            sm.Type = adTypeBinary
            sm.Open
            sm.Write (rs_two.Fields(0).Value)
            If Right(App.Path, 1) = "\" Then
              sm.SaveToFile App.Path & "图标\full.ico", adSaveCreateOverWrite
            Else
              sm.SaveToFile App.Path & "\" & "图标\full.ico", adSaveCreateOverWrite
            End If
            If Right(App.Path, 1) = "\" Then
              Image4(a).Picture = LoadPicture(App.Path & "图标\full.ico")
            Else
              Image4(a).Picture = LoadPicture(App.Path & "\" & "图标\full.ico")
            End If
            sm.Close
            rs_two.Close
            conn_Two.Close