先建立工程,添加控件savedialog,opendialog,adoquery,edit,button1,button2,
属性:button1->caption=“存储文件”,button2->caption=“输出文件”
表的结构:
bh int 4 0
wj image 16 1
wjm varchar 50 1
kzm char 10 1
事件:button1click()
{
oleVariant Data ;
if (OpenDialog1->Execute())
{
TMemoryStream* FS = new TMemoryStream();
FS->Clear();
FS->LoadFromFile(OpenDialog1->FileName);
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("Insert INTO savefile(wj,wjm,kzm)"
;
ADOQuery1->SQL->Add("VALUES
a,:b,:c)"
;
try
{ Data=StreamToVariant(FS);
FS->Position = 0;
ADOQuery1->Parameters->ParamByName("b"
->Value=ExtractFileName (OpenDialog1->FileName);
ADOQuery1->Parameters->ParamByName("c"
->Value=ExtractFileExt(OpenDialog1- >FileName);
ADOQuery1->Parameters->ParamByName("a"
->LoadFromStream(FS,ftBlob);
ADOQuery1->ExecSQL();
Application->MessageBox("恭喜你文件提交成功!","提示",64);
}
__finally
{//FS->Free();
delete FS;}
};
}
button2click()
{
OleVariant Data;
TMemoryStream* FS = new TMemoryStream();
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("select * from savefile where wjm=:a"
;
ADOQuery1->Parameters->ParamByName("a"
->Value=Edit1->Text;
ADOQuery1->Prepared;
ADOQuery1->Open();
ADOQuery1->First();
if (ADOQuery1->Eof)
Application->MessageBox("没有符合条件的记录!","错误",16);
else
{
if ((TBlobField*)(ADOQuery1->FieldByName("wj"
)->IsNull)
Application->MessageBox("文件不存在!","错误",16);
else
{
try
{ if (SaveDialog1->Execute())
{
((TBlobField*)(ADOQuery1->FieldByName("wj"
))->SaveToStream(FS);
FS->Position = 0;
FS->SaveToFile(SaveDialog1->FileName);
Application->MessageBox("下载成功!","提示",64);
} }
__finally
{ delete FS;}
};
}
}
调用的函数:
void VariantToStream(const Variant Data,TStream *Stream)
{
void *p;
p = VarArrayLock(Data);
try
{
Stream->Write(p, VarArrayHighBound(Data,1) + 1);
}
__finally
{
VarArrayUnlock(Data);
}
}
Variant StreamToVariant(TStream *Stream)
{
void *p;
Variant Result;
int Bounds[2]={0, Stream->Size - 1};
Result = VarArrayCreate(Bounds,1, varByte);
p = VarArrayLock(Result);
try
{
Stream->Position = 0;
Stream->Read(p, Stream->Size);
}
__finally
{
VarArrayUnlock(Result);
}
return Result;
}