to:美国提子、昱昱
我按照你们的代码,用ADO连接SQL 7.0数据库,运行时也会出现错误提示:
"Bitmap image is not valid"
我的完整测试代码如下,请继续指点:
unit photops;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, DBCtrls, DB, ADODB, ExtDlgs,jpeg;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
DataSource1: TDataSource;
DBImage1: TDBImage;
DBNavigator1: TDBNavigator;
BitBtn1: TBitBtn;
OpenPictureDialog1: TOpenPictureDialog;
OpenDialog1: TOpenDialog;
procedure BitBtn1Click(Sender: TObject);
private
// function BlobContentToString(const FileName:String):String;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
sFileName:String;
function BlobContentToString(const FileName:String):String;
begin
with TFileStream.Create(FileName,fmOpenRead) do
try
SetLength(Result,Size);
Read(Pointer(Result)^,Size);
finally
Free;
end;
end;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
adotable1.Open;
opendialog1.Filter:='*.jpg|*.jpg';
if (OpenDialog1.Execute) then
begin
sFileName:=OpenDialog1.FileName;
ADOtable1.Insert;
ADOtable1.FieldByName('photo').AsString:=BlobContentToString(sFileName);
adotable1.Post;
end;
end;
end.