An error occurred while reading the file from the SQL database. Is it urgent or online? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiDB/html/delphi_20061222113003166.html
Ladies and Gentlemen:
I already have a field in the SQL database that stores the Excel file, which is of the image type. Now, the data is obtained through the stored procedure, but an error occurs when I want to open the retrieved Excel file. Code As follows:
VaR
Estream: tadoblobstream;
Clientdataset1.close;
Clientdataset1.command: = 'exec Stored Procedure name ';
Clientdataset1.open;
Estream: = tadoblobstream. Create (clientdataset1.fieldbyname ('t_ Excel ') as tblobfield), bmread );
Estream. savetofile ('C: \ 123.xls ');
Estream. Free;
However, an error occurred while running: 'invalid class typecast'
Please help! Thank you!
Compare the file headers before and after file loading with the hexadecimal editor.
Find the cause.
It may be useful to provide you with a document.
One annoying aspect of SQL Server and Delphi is that it is annoying to store arbitrary binary files. For example, you can save a Word file (25 KB) to SQL Server, but when you try to save it as another file, the size of the generated file may be around 24.7kb! In this way, the word cannot open the re-generated file!
To solve this problem, make several modifications:
First, you must modify the type of the field used to save any files in SQL Server to image (do not use the text/ntext field, or use the binary/varbinary field, because binary fields can only store up to 8 K of data) data type, then, when you add a field in word, Delphi will default the field type to ftmemo, you must change this field to the ftblob field! You can use
Table1filefield. loadfromfile ()/savetofile () to read arbitrary binary files! And no bytes will be lost! There will be nothing more! This is an example of kingron's experience.
Without stream, save it to the database, read it, and save it to a file.
Save:
Tblobfield (cdserfiles. fieldbyname ('filecontent'). loadfromfile (opendialog. filename );
Read:
Tblobfield (cdserfiles. fieldbyname ('filecontent'). savetofile (sfilename );
The problem has been solved. Thank you! Paste and score