十万火急!求救!关于存储图片到sql

myworlds 2003-02-22 10:34:00
我新建了一个ado控件,把它和Sql的数据库连接了起来
但是我不知道怎么通过VB界面把图片保存到Sql那里,
Sql数据库那里的表是table1,保存图片的字段是photo,
我用command1按钮把硬盘上的图片先放到image1控件里面
然后我想通过command2按钮把图片保存到sql当前显示的记录的那个image字段,
请问我应该怎么写command2_click事件?我看到网上写了很多子过程,不过我看不太懂,希望有朋友能帮一下我,非常感谢!
...全文
54 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
foreverforyou 2003-03-18
  • 打赏
  • 举报
回复
Public Sub SavePic(ByVal pPicPath As String)
Dim Rs As New ADODB.Recordset
dim adoCn As New ADODB.Connection
Dim bytPicData() As Byte
dim strCn As String

Open pPicPath For Binary As #1
ReDim bytPicData(LOF(1) - 1)
Get #1, , bytPicData()
Close #1

strCn = "Provider=SQLOLEDB.1;User ID=sa;Initial Catalog=databasename;Data Source=Server;Password=;"
adoCn.Open strCn

Rs.Open "SELECT * FROM dPicture", adoCn, adOpenKeyset, adLockOptimistic
Rs("PicData").AppendChunk bytPicData()
Rs.Update

Set Rs = Nothing
End Sub
jadehong 2003-03-18
  • 打赏
  • 举报
回复
Option Explicit
Dim conn As ADODB.Connection
Dim Chunk() As Byte
Dim lngLengh As Long
Dim intChunks As Integer
Dim intFragment As Integer
Const ChunkSize = 1000
Const lngDataFile = 1

Private Sub Savepic()
Open "c:\YOU.gif" For Binary Access Read As lngDataFile
lngLengh = LOF(lngDataFile)
If lngLengh = 0 Then Close lngDataFile: Exit Sub
intChunks = lngLengh \ ChunkSize
intFragment = lngLengh Mod ChunkSize

'OpenData 打开数据库
Dim i As Integer
Dim rs As New ADODB.Recordset
Dim strQ As String

If rs.State = adStateOpen Then rs.Close

strQ = "Select * From [mydata]"
rs.Open strQ, conn, adOpenStatic, adLockOptimistic

On Error Resume Next

rs.AddNew

ReDim Chunk(intFragment)
Get lngDataFile, , Chunk()
rs.Fields("rs_photo1").AppendChunk Chunk()
ReDim Chunk(ChunkSize)

For i = 1 To intChunks
Get lngDataFile, , Chunk()
rs.Fields("rs_photo1").AppendChunk Chunk()
Next i

rs.Update
rs.Close
Close lngDataFile
Call ShowPic
End Sub

Public Sub ShowPic()
'OpenData 打开数据库
Dim i As Integer
Dim rs As New ADODB.Recordset
Dim strQ, filename As String
If rs.State = adStateOpen Then rs.Close

strQ = "Select * From [mydata]"
rs.Open strQ, conn, adOpenStatic, adLockOptimistic
If rs.EOF <> True Then
rs.MoveLast
Else
Exit Sub
End If
On Error Resume Next
Open "pictemp" For Binary Access Write As lngDataFile
lngLengh = rs.Fields("rs_photo1").ActualSize
intChunks = lngLengh \ ChunkSize
intFragment = lngLengh Mod ChunkSize
ReDim Chunk(intFragment)
Chunk() = rs.Fields("rs_photo1").GetChunk(intFragment)
Put lngDataFile, , Chunk()
For i = 1 To intChunks
ReDim Buffer(ChunkSize)
Chunk() = rs.Fields("rs_photo1").GetChunk(ChunkSize)
Put lngDataFile, , Chunk()
Next i
Close lngDataFile
filename = "pictemp"
Picture1.Picture = LoadPicture(filename)
Image1.Stretch = True
Image1.Picture = Picture1.Picture
Kill filename
End Sub
WANGHUIBIN 2003-02-25
  • 打赏
  • 举报
回复

Private Sub Form_Unload(Cancel As Integer)
If rs.State = adStateOpen Then
rs.Close
End If

If cn.State = adStateOpen Then
cn.Close
End If
End
End Sub
WANGHUIBIN 2003-02-25
  • 打赏
  • 举报
回复

Private Sub Form_Load()
On Error GoTo ErrHandler:

Dim UserName As String
Dim Password As String
Dim ServerName As String
Dim DBName As String

UserName = ""
Password = ""
ServerName = ""
DBName = "Northwind"

' Set connection properties.
cn.ConnectionTimeout = 25 ' Set the time out.
cn.Provider = "sqloledb" ' Specify the OLE DB provider.
cn.Properties("Data Source").Value = ServerName ' Set SQLOLEDB connection properties.
cn.Properties("Initial Catalog").Value = DBName ' Set SQLOLEDB connection properties.
cn.Properties("Integrated Security").Value = "SSPI" ' Set SQLOLEDB connection properties.

' Change mousepointer while trying to open database.
Screen.MousePointer = vbHourglass

' Open the database.
cn.Open

' Open the Recordset.
Set rs = New ADODB.Recordset
rs.Open "select * from Employees", cn, adOpenKeyset, adLockPessimistic

' Move to the first record and display the data.
rs.MoveFirst
FillDataFields

' Change mousepointer back to the default after open.
Screen.MousePointer = vbDefault

Exit Sub

ErrHandler:
' Change mousepointer back to the default after open.
Screen.MousePointer = vbDefault

' Display the error message.
MsgBox Err.Description, , "Error "

' End the program.
End
End Sub
WANGHUIBIN 2003-02-25
  • 打赏
  • 举报
回复
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Employee Code Sample
' Microsoft Corporation ''来自微软的官方事例
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧