An error occurred while reading the file from the SQL database. Is it urgent or online?

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.