???? ID: 194975 - ????? ???????: 03 ?????? 2010 - ??????: 4.0
????? ?? ????? ???? ????? GetChunk ?? AppendChunk BLOBs
?????? ?????? This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
?? ???? ?? Microsoft Visual Basic .NET ??????? ?? ???, ?????
317034
(http://support.microsoft.com/kb/317034/EN-US/
)
.
?? ???? ?? Microsoft Visual Basic .NET ??????? ?? ???, ?????
316887
(http://support.microsoft.com/kb/316887/EN-US/
)
.
?? ???? ????? ???? ?? ?? ???? ??? ?? ??????? ??? ???????? (BLOBs) ADO ??? ????? ?? ??????? GetChunk ?? AppendChunk ??????? ?? ????? ?? ?????? ????? ????? ?? ????? ??? NWIND ????? ??????? ?? ????? ?? ??? ???
GetChunk ?? AppendChunk ??????? ??? LongVarChar, LongVarWChar ?? LongVarBinary ????? ??????, ?? ???? ???, NTEXT ?? ??? ?????, Microsoft SQL Server ???, ?? Microsoft Jet ??????? ??? ???? ?? OLE ????????? ?? ??? ???? ?? ??? adLongVarChar, adLongVarWChar ?? adLongVarBinary ?? ??? ??? ????? ??? ?????? ?? ??????? ???? ?? ADO ??? ?? ??????? ?? ????? ????? ?? adFldLong ???? ?? ??? ??? ????? ?? ??????? ??? ?? ??????? ?? ?? ???? ???:
If fld.Attributes And adFldLong Then
' You can use GetChunk/AppendChunk
???? ????? ??? ??????? ??? ?? ???????? BLOBs (??????? ??? ??????????) ?? ??? ??? ??? ?? ?? ??? ???? ?? ???? ??? ???? ??? ?? ????? ??? ?? ?? routines, BlobToFile ?? FileToBlob ???? ???
BlobToFile BlobToFile ????? ?? ???? ?????? ?? ?? ????? ???? ????? ?? ???? ???? ?? ????? ?? ????? ???? ?? ??? ??? ??????? ????????? ???? ??? ??? ????? ???? ???????? ???? ??, ?? ?????? ??? ??? ???? entirety GetChunk ??? ??? ???? ?????? ?????? ??? ????? ???? ?????? ??, ?? ??? ????? ??? ???? ????? ?? ??? WriteFromUnsizedBinary ?? WriteFromUnsizedText. ?? ?? WriteFromBinary ?? WriteFromText ?? routines ????? ???? ?? ???? ???? ???? ?? ?? ????? ??? ???? ??? ?? ??????? ?????? ??? ???? ?? ???????? ????????? ????? ?? ????? ??? ?? ???? ??:
BlobToFile Calls one of the below routines to use GetChunk
WriteFromBinary Writes a LongVarBinary of known size to disk
WriteFromUnsizedBinary Writes a LongVarBinary on unknown size
WriteFromText Writes a LongVarChar of known size
WriteFromUnsizedText Writes a LongVarChar of unknown size
FileToBlob FileToBlob AppendChunk ?? ????? ???? ?? ???? ???? ????? ?? ???? ?? ?????? ????? ????? ?? ??? ????? ???? ?? ??? ?? ???? ?? ????????? ???? ??? ????? ?? ???? ????????? ????? ???? ?? ????, ??????? ??? "Unsized" routines ???? ?? ??? ??? BlobToFile ????? ??? ??? ???:
FileToBlob Calls one of the below routines to use AppendChunk
ReadToBinary Reads a file into a LongVarBinary column
ReadToText Reads a file into a LongVarChar column
??????: ????? ??? ?? BlobToFile ?? FileToBlob ?? ??? ???? ??????? ??? ???????? ?? ?? ??????? ??????? ?? ???? ??????? ??? ??? ??????? ??? ??? ????????? ?? ???????? ????? (???/OLE/LongVarBinary) ????? ?? ?????? ?? ??? ????? ???? ?? ?? ??????????? ?????? NWIND ??????? ?? (???/????/LongVarChar/LongVarWChar) ????? Andrew Fuller ?? ??? ??? ????? ?? ?? reads ??????? ??? ????? ??, ?? ???????? ???? ?? ?? ?????? ?? ??????? ?? ??? ??? ??????? ?? ????? ??? ??? ??, ?? ?? ??????? ????? ???
???: : ????? ADO 2.1 ?? ??? ???, ?? ???? ????? ????? ?????? ??? ?? ?????? ??:
Data = fld.GetChunk(BLOCK_SIZE) :
Run-time error '94':
Invalid use of Null
ADO 2.1 ?? ??? ??? ?? ??? ?????? ?? ?????? ?? ??? ??? ActualSize ??? ?? ??? ?????? ????? ????? ?? ??????? ?? ???? ??? ?? ??? ?? ??? ????? ????? ??? ??????? ???, ??????? ??????? ???? ?? ????? 2 ?????? ?? ????? ???? ??? ?? ??? ???? ?? ??? ????? ????? ??? ANSI ???, ?? 1-???? ????? ?? ????? ?? ????? ???? ??? ??? ActualSize ?? ?? ANSI ????? ?? ?? ??? ????? ??, ?? GetChunk ????? ?????? ????? ?? ??? ??? ????? ??????? ???? ?? ????
?? ????? ?????????? ??? ?? ??????? ??? ???? ???, ?? ???? ???????? ??? ??? ??? ???? ??:
1 ???????? :
???? SQL ????? 7 NTEXT ????? (ANSI ???), ?? ??? SQL ????? ?? SQL Server ODBC ??????? ?? ??? ODBC ??????? ?? ??? ?? ?? OLE DB ??????? ?? ????? ???
??? ???? ?? SQL ????? ??? ?? ????? ?? ????? (???????) ??? ????? ????????? ?? ???
???????? 2 :
Access 97 ???? ?????? ?? ??? ?? Access 2000 ???? ?????? ?? ??? ?? ???? ??????? ??????? ?? ???, Jet 4.0 ?? ??? ODBC 4.0 ???????, ODBCJT32.DLL ?? ??? ODBC ??????? ?? ??? ?? ?? OLE DB ??????? ?? ????? ???
???, ?? Access 97 ???? ?????, ????? OLE DB ??????? ??? 3.51 ?? ??? ODBC 3.51 ??????? ????? ?? ??? ODBC ??????? ?? ??? ??? ???
??-???? ?????? '94' ?? ??? ?? ??????? ?????? ???:
Rs.Fieldname.ActualSize ?? ????? ???? \ rs.Fieldname.ActualSize ?? ????? ?? 2?
?? ?? ???? ???????? ??????? ???????? ?? ??? ???????? ????? ???? 2 ?? 3 ???? ?? ??, ?? ?? ?? ?? ActualSize ??? ?? ?????? ?? ????? ????? ???? ?? ?????? ?? ??? ?? Microsoft Access ?? ???? ?????, ??? NWIND.MDB ?????? ??????????? ?? ?????? (?? ???????) ?? ????? ?? "Andrew Fuller." ?? ??? ??? ??? ?????? ??? ?? ??? ??? ????? (???? 60000 ?? 30000 ??????) ?? ??????? ?? ???????? ?????????? ?? ?????? ?? ????? ?? ???? ??? ???? ???? ODBC ????? NWIND.MDB ????? ?? ????? ???? ?? ?? ??????? ????? ??? ??? ??? Visual Basic ????????? ????? ?? ????????? ???? ?? ?????? ?? ??? ????, ?? Microsoft ActiveX ???? ???????? ????????? ?? Microsoft ActiveX ???? ???????? ????????? ?? ??? ????? ??????? form(Form1) ???? ?? ??? ?? CommandButtons (cmdSave ?? cmdLoad) ??????? ????? ??? ??????? ?? "cn.Open" ???? ?? ???? ??? ?????? ???????? ????????? ???? ?? ??? ????:
Option Explicit
Private Sub CmdSave_Click()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, SQL As String
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.CursorLocation = adUseServer
cn.Open "dsn=nwind_jet" ' *** change this ***
SQL = "SELECT * FROM Employees WHERE LastName='Fuller'"
rs.Open SQL, cn, adOpenStatic, adLockReadOnly
'
' Save using GetChunk and known size.
' FieldSize (ActualSize) > Threshold arg (16384)
'
BlobToFile rs!Photo, "c:\photo1.dat", rs!Photo.ActualSize, 16384
BlobToFile rs!Notes, "c:\notes1.txt", rs!Notes.ActualSize, 16384
' Uncomment the next line of code, and comment the line above,
' to workaround Runtime error '94': Invalid use of Null
' BlobToFile rs!Notes, "c:\notes1.txt", rs!Notes.ActualSize \ 2, 16384
'
' Save using GetChunk and unknown size.
' FieldSize not specified.
'
BlobToFile rs!Photo, "c:\photo2.dat"
BlobToFile rs!Notes, "c:\notes2.txt"
'
' Save without using GetChunk
' FieldSize (ActualSize) < Threshold arg (defaults to 1Mb)
'
BlobToFile rs!Photo, "c:\photo3.dat", rs!Photo.ActualSize
BlobToFile rs!Notes, "c:\notes3.txt", rs!Notes.ActualSize
' Uncomment the next line of code, and comment the line above,
' to workaround Runtime error '94': Invalid use of Null
' BlobToFile rs!Notes, "c:\notes3.txt", rs!Notes.ActualSize \ 2
rs.Close
cn.Close
End Sub
Private Sub CmdLoad_Click()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, SQL As String
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.CursorLocation = adUseServer
cn.Open "dsn=ole_db_nwind_jet"
SQL = "SELECT * FROM Employees"
rs.Open SQL, cn, adOpenKeyset, adLockOptimistic
'
' Load using AppendChunk
'
rs.AddNew
rs!FirstName = "Test"
rs!LastName = "Fuller11"
FileToBlob "c:\photo1.dat", rs!Photo, 16384
FileToBlob "c:\notes1.txt", rs!Notes, 16384
rs.Update
rs.AddNew
rs!FirstName = "Test"
rs!LastName = "Fuller21"
FileToBlob "c:\photo2.dat", rs!Photo, 16384
FileToBlob "c:\notes2.txt", rs!Notes, 16384
rs.Update
rs.AddNew
rs!FirstName = "Test"
rs!LastName = "Fuller31"
FileToBlob "c:\photo3.dat", rs!Photo, 16384
FileToBlob "c:\notes3.txt", rs!Notes, 16384
rs.Update
'
' Load without using AppendChunk
'
rs.AddNew
rs!FirstName = "Test"
rs!LastName = "Fuller12"
FileToBlob "c:\photo1.dat", rs!Photo
FileToBlob "c:\notes1.txt", rs!Notes
rs.Update
rs.AddNew
rs!FirstName = "Test"
rs!LastName = "Fuller22"
FileToBlob "c:\photo2.dat", rs!Photo
FileToBlob "c:\notes2.txt", rs!Notes
rs.Update
rs.AddNew
rs!FirstName = "Test"
rs!LastName = "Fuller32"
FileToBlob "c:\photo3.dat", rs!Photo
FileToBlob "c:\notes3.txt", rs!Notes
rs.Update
rs.Close
cn.Close
End Sub
????? ??? ?? ??? ????????? (Module1) ?? ??? ??? ??? ??????? ?????:
Option Explicit
Const BLOCK_SIZE = 16384
Sub BlobToFile(fld As ADODB.Field, ByVal FName As String, _
Optional FieldSize As Long = -1, _
Optional Threshold As Long = 1048576)
'
' Assumes file does not exist
' Data cannot exceed approx. 2Gb in size
'
Dim F As Long, bData() As Byte, sData As String
F = FreeFile
Open FName For Binary As #F
Select Case fld.Type
Case adLongVarBinary
If FieldSize = -1 Then ' blob field is of unknown size
WriteFromUnsizedBinary F, fld
Else ' blob field is of known size
If FieldSize > Threshold Then ' very large actual data
WriteFromBinary F, fld, FieldSize
Else ' smallish actual data
bData = fld.Value
Put #F, , bData ' PUT tacks on overhead if use fld.Value
End If
End If
Case adLongVarChar, adLongVarWChar
If FieldSize = -1 Then
WriteFromUnsizedText F, fld
Else
If FieldSize > Threshold Then
WriteFromText F, fld, FieldSize
Else
sData = fld.Value
Put #F, , sData ' PUT tacks on overhead if use fld.Value
End If
End If
End Select
Close #F
End Sub
Sub WriteFromBinary(ByVal F As Long, fld As ADODB.Field, _
ByVal FieldSize As Long)
Dim Data() As Byte, BytesRead As Long
Do While FieldSize <> BytesRead
If FieldSize - BytesRead < BLOCK_SIZE Then
Data = fld.GetChunk(FieldSize - BLOCK_SIZE)
BytesRead = FieldSize
Else
Data = fld.GetChunk(BLOCK_SIZE)
BytesRead = BytesRead + BLOCK_SIZE
End If
Put #F, , Data
Loop
End Sub
Sub WriteFromUnsizedBinary(ByVal F As Long, fld As ADODB.Field)
Dim Data() As Byte, Temp As Variant
Do
Temp = fld.GetChunk(BLOCK_SIZE)
If IsNull(Temp) Then Exit Do
Data = Temp
Put #F, , Data
Loop While LenB(Temp) = BLOCK_SIZE
End Sub
Sub WriteFromText(ByVal F As Long, fld As ADODB.Field, _
ByVal FieldSize As Long)
Dim Data As String, CharsRead As Long
Do While FieldSize <> CharsRead
If FieldSize - CharsRead < BLOCK_SIZE Then
Data = fld.GetChunk(FieldSize - BLOCK_SIZE)
CharsRead = FieldSize
Else
Data = fld.GetChunk(BLOCK_SIZE)
CharsRead = CharsRead + BLOCK_SIZE
End If
Put #F, , Data
Loop
End Sub
Sub WriteFromUnsizedText(ByVal F As Long, fld As ADODB.Field)
Dim Data As String, Temp As Variant
Do
Temp = fld.GetChunk(BLOCK_SIZE)
If IsNull(Temp) Then Exit Do
Data = Temp
Put #F, , Data
Loop While Len(Temp) = BLOCK_SIZE
End Sub
Sub FileToBlob(ByVal FName As String, fld As ADODB.Field, _
Optional Threshold As Long = 1048576)
'
' Assumes file exists
' Assumes calling routine does the UPDATE
' File cannot exceed approx. 2Gb in size
'
Dim F As Long, Data() As Byte, FileSize As Long
F = FreeFile
Open FName For Binary As #F
FileSize = LOF(F)
Select Case fld.Type
Case adLongVarBinary
If FileSize > Threshold Then
ReadToBinary F, fld, FileSize
Else
Data = InputB(FileSize, F)
fld.Value = Data
End If
Case adLongVarChar, adLongVarWChar
If FileSize > Threshold Then
ReadToText F, fld, FileSize
Else
fld.Value = Input(FileSize, F)
End If
End Select
Close #F
End Sub
Sub ReadToBinary(ByVal F As Long, fld As ADODB.Field, _
ByVal FileSize As Long)
Dim Data() As Byte, BytesRead As Long
Do While FileSize <> BytesRead
If FileSize - BytesRead < BLOCK_SIZE Then
Data = InputB(FileSize - BytesRead, F)
BytesRead = FileSize
Else
Data = InputB(BLOCK_SIZE, F)
BytesRead = BytesRead + BLOCK_SIZE
End If
fld.AppendChunk Data
Loop
End Sub
Sub ReadToText(ByVal F As Long, fld As ADODB.Field, _
ByVal FileSize As Long)
Dim Data As String, CharsRead As Long
Do While FileSize <> CharsRead
If FileSize - CharsRead < BLOCK_SIZE Then
Data = Input(FileSize - CharsRead, F)
CharsRead = FileSize
Else
Data = Input(BLOCK_SIZE, F)
CharsRead = CharsRead + BLOCK_SIZE
End If
fld.AppendChunk Data
Loop
End Sub
????????? ?? ?????, ?? cmdSave ??? ????? ????? C:\ ?????????? ??? ???? ????? ?????? ?????? ???? ?????: notes1.txt notes2.txt notes3.txt photo1.dat photo2.dat photo3.dat ??? "?????" ??????? ?? ???? ???? ?? ??-????? ?? ??? ??? ???? ?????? ??? "??? ????" ??????? ?? ???? ???? ?? ??-????? ?? ??? ??? ???? ?????? CmdLoad ??? ????? ????? ???? ??????? ?? Access ?? ?? ????? ???? ????? ????? ??????? ?? ??? ??? ???? ??? ??? ?? ?? ???????? ???????????? ??? ????? ??? ?? ADO BLOBs ?? ????? ???? ?? ??? ??? ????? ???? ?? ????? ?? ????? Microsoft ???????? ???? ??? ?? ????????:
153238
(http://support.microsoft.com/kb/153238/EN-US/
)
GetChunk ?? RDO ???????? ?? AppendChunk ????????? ?? ????? ???? ????
?? ?? ?? ??? ?? ???? ????? ??????? ??? ????? ?? ??????? ???? ?????? ???? ?? ??? ????? ???? ?? ????????????? ?? ????? ??? ???? ???? ?? (?? ??? ????? ?????? ??????????/????? ?????? ?? ???? ?? ???????? ????? ??? ?????? ?? ????? ?? ???? ???)? ?? ??? (???) ????? overhead eliminating, (b) ??????? ?? ???? ????? ????? ?? ???????? ???? ?? ??? ?????? ??, ?? (c) ??? ??????? ??????? ????????? ?? ???-??? ??????? ?? ??? ???? ?? ??? ????, ?? (d) ????? ??????? ?? ????????????? ?? ????? ???? ?? ???? ?? ??? ?? ??????? ???? ?? ??? ??????? ??? ?? ???????? ??????, ???? ?????? (. bmp), ???? ???????? ??????? (.doc), ?? ?????????? (.xls) ?? ??? ????? ???? ???? ?? ????? ????????? ????? ?? ????? ?? ????? ?? ???? ???? ??? ?????????, ???? notably ODBC SQL ????? ?? ???? ??????? ?? ????? ???? ??? ???? ????? ???? ???????????? ?? ??? ??, ???? ????? ????? ????? ???? ?? ??? ??? ???? ?? ????? ????? ??? ?????? ???? ?? ???? ??? ???-????? ?????? ?? ???????? ??? ????? care ???? ?? ??? ?? ?? ???? ??? ?? ?????? ?? ?????? ???? ?????? ?? ????, ????:??????? (?????????? ODBC) ???-??? ????? ????? ????? (?????????? ???????) ????? ?????? ???? ?? ??? ??? ?? ?? ????? ?? ??? ???? ?? ??? ???????? ????????? ?? ?? ??????? ?? ??? ??? ??????? ?? ??????? ????? ?? ?????? ???? ??, ???? ?? ?? ???????????? ??? ???????? ????? ??????? ???? ???:
??? ODBC ??????? ?? ???? ???? ??????? OLE DB ??????? ?? ?????? ????? ?????-???? ????? ?? (???? adOpenKeyset) ?? ????? ????? ???? ????? ?? ????? ???????? ????? column(s) ?? ??? ????? ????? ??? ????? ??????? ?? ??? ????? ????, ???-??? ????? ?? ??? ???? "*"? ??? ???-????? ??????? ?? ???? ??????? (?????? ?????? ??? ?????? ??)? ????????? ???? ??? ????? ????? ?????? ?? ?????? ??? ?? ??? ???? ?? ???? ????? ??? ???? ??? ?? ???? ?? ???? ??? ?? ??? ????? ????? AppendChunk ???? ?? ????? ?? ??????? ????, ???? ?? ???? recordset ??? ?? ?? ?? ?? ???-????? ????? ?? ??????? ???? ?? ??? ?? ?? ???? ??? BLOBs ?????????? updateable ??????? ?? ODBC datasources ?? ????-????????? ????? ?? ??? ???? ???? ??? ?? ODBC ???? ?? ??? Jet ?? ????? ???? recordset ?? ???????? ???????????? (QueryDef) ?? ?? ??????? forces ?????? ????-????? ?? ??? ???? ?? ???? ?? ??????? ?????? ???? ?? ???? ??? Microsoft Oracle OLE DB ??????? ?? ?????? ???? ???? ??? ?????-???? ????? - ?? ??? ????? ???? ?? ??? ????? ????? ????? ????? ?? ??? ???? ??? ?? ??? ??? ????? ????? ODBC ????? ????????? ?? ?? ???? ???? ?? ?? ??? recordset ?? ??? ???????? ????????? ?? ??? ?? GetChunk ?? AppendChunk ??????? ?? ????? ???? ?? ???? ?? ??????? ????? ???? ?? ??????? ??? ?? ??????? ???? ??? ??? ???? ?? ??? ????????? ?? ?????? ?? ??? ??? ??? ???????? ????????? ?? ?? recordset ????? ??, ?? ????? ??????? ?? ??? ?????? ????? ???? ???? ?? ??? ??? ?? ????? ???? ?? ??? ????? ??????? ?? ???????? ?? ???? ?? ????????? ???? ?? ???? ??????? ???? ????????? ???? ?? ???? ??? ?????-???? ????? ?? ?????? ?? ?? ?? ????? ?? ???????? ????????? (???? SQL Server ????????) ?? ????? ??? ??? ??? ??? ????? ???
???? ?? ?? ???? ????? ????? ?? ?????? ???? ?? ??? ?????????? ????? ?? ?? ???? ???? ???????? ?????? ?? ?? ???? ?????? ?? ???? ?? ??? ???? ??? ?? ????? ???? ??? ????? ?? ????? ?? ??? demands. ?? ???? ?? ???? ??? ?? ODBC (???? ??? ADO thing) ?? ?????? ??? ?? ???
???????? ??????? ?? ???, ????? ????? ?????? ?? Microsoft ???????? ??? ?????:
185958
(http://support.microsoft.com/kb/185958/EN-US/
)
ADO GetChunk/AppendChunk ????? ???? ?? ??? Oracle ?? ??? ?? ?????? ???? ????
189415
(http://support.microsoft.com/kb/189415/EN-US/
)
FILE: ????? C++ ??? GetChunk ?? AppendChunk ????? AdoChunk.exe
???? ????? ???????? ?? ????? ????:
103257
(http://support.microsoft.com/kb/103257/EN-US/
)
ACC: ???, ???????? ????, ?? ??????? ??? ???????? (BLOBs) ??? ????
???? ???? ???? ??: Microsoft ActiveX Data Objects 2.7 Microsoft Visual Basic 6.0 Professional Edition kbbug kbdatabase kbhowto kbmt KB194975 KbMthi
???? ?????? ???????? ??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:
194975
(http://support.microsoft.com/kb/194975/en-us/
)
Was this information helpful?
How much effort did you personally put forth to use this article?
Tell us why and what can we do to improve this information
Thank you! Your feedback is used to help us improve our support content. For more assistance options, please visit the
Help and Support Home Page .
???? ?????? ???? ?????? ??????
??????
??? ?????? ??????? ????
???? ??????