MS Sql server中資料可以Blob欄位為image 型別。把檔案讀取后嗎,寫入資料庫中blob欄位大小只有32767位元組。但我在Sybase SQL 資料庫中一切正常,換作MS SQL server 就是寫入不完整,SQL 2000,2008都試了,都是這個原因。我讀取檔案寫入資料的代碼如下,請高手指點一二:
string ls_pathname, ls_filename
integer li_value,li_FileNum,loops,i
long ll_fileLength,bytes_read,new_pos
blob b, tot_b
Blob gb_photo
string ls_id
li_value = GetFileOpenName("選擇照片", ls_pathname, ls_filename)//
IF li_value = 1 THEN
SetPointer(HourGlass!)
// Get the file length, and open the file
ll_fileLength= FileLength(ls_filename)
li_FileNum = FileOpen(ls_filename,StreamMode!, Read!, LockRead!)
IF ll_filelength > 32765 THEN
IF Mod(ll_filelength, 32765) = 0 THEN
loops =ll_filelength/32765
ELSE
loops = (ll_filelength/32765) + 1
END IF
ELSE
loops = 1
END IF
// Read the file
new_pos = 1
FOR i = 1 to loops
bytes_read = FileRead(li_FileNum, b)
tot_b = tot_b + b
//
// IF i=1 then
// st_1.text=string(tot_b)
// end if
//
NEXT
FileClose(li_FileNum)
gb_photo=tot_b
st_1.text = string(tot_b)
//ole_1.objectdata = gb_photo
// st_1.text = filepath
ls_id = dw_1.getitemstring(dw_1.getrow(),"id")
st_1.text = string(dw_1.getrow())
//檔案存入資料庫
SQLCA.AutoCommit = True
UpdateBlob ole Set object =:gb_photo where id=:ls_id;
if sqlca.sqlcode=0 then
commit;
messagebox("恭喜您","保存成功")
else
rollback;
messagebox("資料庫錯誤","保存失敗")
end if
SQLCA.AutoCommit = false
else
messagebox("提示框","沒選擇檔案!")
end if
uj5u.com熱心網友回復:
知道原因了,原來是資料庫連接方式的問題。原來采用的是ODBC連接方式,改成用專用連接方式就解決了。uj5u.com熱心網友回復:
如果用PB10以上版本,建議用FileReadEx,可以一次直接讀取,不用回圈處理了Description
Reads data from the file associated with the specified file number, which was assigned to the file with the FileOpen function.
Syntax
FileReadEx ( file#, blob {, length } )
FileReadEx ( file#, string )
Argument Description
file# The integer assigned to the file when it was opened.
blob or string The name of the string or blob variable into which you want to read the data.
length In text or stream mode, the number of bytes a retrieve requires. The default value is the length of the file.
Return value
Long. Returns the number of bytes read. If an end-of-file mark (EOF) is encountered before any characters are read, FileReadEx returns -100. If the file is opened in LineMode and a CR or LF is encountered before any characters are read, FileReadEx returns 0. If an error occurs, FileReadEx returns -1. FileReadEx returns -1 if you attempt to read from a string in stream mode or read from a blob in line mode. If any argument's value is null, FileReadEx returns null.
FileReadEx returns long Unlike the FileRead function that it replaces, the FileReadEx function returns a long value.
Usage
FileReadEx can read files with ANSI, UTF-8, UTF-16LE, and UTF-16BE encoding.
If the file is opened in line mode, FileReadEx reads a line of the file (that is, until it encounters a CR, LF, or EOF). It stores the contents of the line in the specified variable, skips the line-end characters, and positions the file pointer at the beginning of the next line.
The optional length parameter applies only to blob data. If the length parameter is provided when the datatype of the second parameter is string, the code will not compile.
If the file was opened in stream or text mode, FileReadEx reads to the end of the file or the next length bytes, whichever is shorter. FileReadEx begins reading at the file pointer, which is positioned at the beginning of the file when the file is opened for reading. If the file is longer than length bytes, FileReadEx automatically positions the pointer after each read operation so that it is ready to read the next chunk of data.
An end-of-file mark is a null character (ASCII value 0). Therefore, if the file being read contains null characters, FileReadEx stops reading at the first null character, interpreting it as the end of the file.
If the file is an ANSI or UTF-8 file and is read into a string, FileReadEx converts the text to Unicode before saving it in the string variable. The BOM is not written to the string.
If the file is an ANSI or UTF-8 file and is read into a blob, FileReadEx saves the contents of the file with no conversion. The BOM is not written to the blob in text mode, but it is written to the blob in stream mode.
If the file is in Unicode, no conversion is required.
uj5u.com熱心網友回復:
我用的PB9.0uj5u.com熱心網友回復:
pb9轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/68336.html
標籤:數據庫相關
上一篇:PB9.0怎么實作在主視窗顯示一條記錄,雙擊后打開明細視窗
下一篇:“酸甜苦辣咸”的“面霸哥”
