c mysql 二进制图片,想把二进制数据插入到MYSQL库里,字段为MediumBLOB类型.(存的图片)解决思路...

当前位置:我的异常网» VB » 想把二进制数据插入到MYSQL库里,字段为MediumBLOB类

想把二进制数据插入到MYSQL库里,字段为MediumBLOB类型.(存的图片)解决思路

www.myexceptions.net  网友分享于:2013-01-03  浏览:281次

想把二进制数据插入到MYSQL库里,字段为MediumBLOB类型.(存的图片)

查到的资料:

如果你想要把二进制数据插入到一个字符类型的字段中(例如BLOB),下列字符必须由转义序列表示:

NUL

ASCII   0,你应该用   “\0”(一个反斜线和一个ASCII   “0”字符)表示它。

\

ASCII   92,反斜线。需要用   “\\”   表示。

'

ASCII   39,单引号。需要用   “\ '”   表示。

"

ASCII   34,双引号。需要用   “\ "”   表示。

-----------------------------------

问题是:请问我如何转义二进制数据.下面里面看不出来有什么引号之类的..

二进制数据是这样格式:????āā怀怀   ????????????????????┝???‖???????┰?H???????????????????????????????????????????!?ā?āā       ā????????????ā???????慑??脲??????戳色???┚??????????呓噕塗婙摣晥桧橩瑳癵硷穹蒃蚅袇誉鎒閔鞖馘???????????????????????笠????āāāā     ā?????????????w???ㄡ?允愇?㈢?????刳?牢??????????????半啔坖奘捚敤杦楨獪畴睶祸艺蒃蚅袇誉鎒閔鞖馘???????????????????????笠?ú?????????薿?饠???????棈????卺???罊?櫎???????坳??汖???駹??鋏???}?????龗???貦?蔽蠟..........

------解决方案--------------------

参考:http://bbs.xplore.cn/viewthread.php?tid=2507

------解决方案--------------------

我以前回答过多次此类问题,但2006年的贴子不能查到了。

你自己修改一下.

VB把文件存入数据库IMAGE字段

Sub Savepic(FileName As String, IndexNumber As Long)

Dim DcnNWind As New ADODB.Connection

Dim rs As ADODB.Recordset

Set rs = New ADODB.Recordset

DcnNWind.CursorLocation = adUseClient

DcnNWind.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER "

rs.CursorType = adOpenKeyset

rs.LockType = adLockOptimistic

rs.Open "CustomInfo ", DcnNWind, , adCmdTable

rs.Move (IndexNumber)

Call FileToBlob(rs.Fields( "Image "), FileName, FileLen(FileName))

rs.UpdateBatch adAffectCurrent

End Sub

Private Sub FileToBlob(fld As ADODB.Field, FileName As String, Optional ChunkSize As Long )

Dim fnum As Integer, bytesLeft As Long, bytes As Long

Dim tmp() As Byte

If (fld.Attributes And adFldLong) = 0 Then

Err.Raise 1001, , "Field doesn 't support the GetChunk method. "

End If

fnum = FreeFile

Open FileName For Binary As fnum

bytesLeft = LOF(fnum)

Do While bytesLeft

bytes = bytesLeft

If bytes > ChunkSize Then bytes = ChunkSize

ReDim tmp(1 To bytes) As Byte

Get #1, , tmp

fld.AppendChunk tmp

bytesLeft = bytesLeft - bytes

Loop

Close #fnum

End Sub

VB把文件从IMAGE字段中读到文件中。

Sub loadpic(IndexNumber As Long)

Dim DcnNWind As New ADODB.Connection

Dim rs As ADODB.Recordset

Set rs = New ADODB.Recordset

DcnNWind.CursorLocation = adUseClient

DcnNWind.Open "Provider=SQLOLEDB.1;Integrated Security=SSI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER "

rs.CursorType = adOpenKeyset

rs.LockType = adLockOptimistic

rs.Open "CustomInfo ", DcnNWind, , adCmdTable

rs.Move (IndexNumber)

Call BlobToFile(rs.Fields( "Image "), "c:\windows\temp\tmp.bmp ", rs.Fields( "Image ").ActualSize)

End Sub

Private Sub BlobToFile(fld As ADODB.Field, FileName As String, Optional ChunkSize As Long )

Dim fnum As Integer, bytesLeft As Long, bytes As Long

Dim tmp() As Byte

If (fld.Attributes And adFldLong) = 0 Then

Err.Raise 1001, , "Field doesn 't support the GetChunk method. "

End If

If Dir$(FileName) <> " " Then Kill FileName

fnum = FreeFile

Open FileName For Binary As fnum

bytesLeft = fld.ActualSize

Do While bytesLeft

bytes = bytesLeft

If bytes > ChunkSize Then bytes = ChunkSize

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值