用TQuery怎样读取数据库中的二进制数据?

huche 2002-08-01 09:53:12
image类型,text类型,sql server7.0


谢谢
...全文
71 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolpony 2002-08-02
这是一个读图片的例子,其他的blob的字段的读取得方法也是一样的。
回复
coolpony 2002-08-02
#include<memory>
using namespace std;

{
adoimagequery->sql->text="select...............";
adoimagequery->open();
std::auto_ptr<TMemorystream> stream (new TMemorystream);
TBlobField* field = dynamic_cast<TBlobField* >(adoimagequery->fieldbyname("image"));
if(field)
{
field->SaveToStream(stream.get);
stream->position = 0;
std::auto_ptr<TJPEGImage>jpeg(new TJPEGImage );
jpeg->LoadFromStream(stream.get);
if(jpeg->hight>0&&jpeg->width>0)
Image->Picture->bitmap->assign(jpeg.get());
else Image->Picture->assign(null);
}
adoimagequery->close();
}
只有就可以把blob的字段读出来。写进数据库就是读的相反过程。。
回复
Bird1945 2002-08-01
Query1->Close();
T = new(TBinaryField);
T->FieldName = "RawData";
T->Name = Query1->Name + T->FieldName;
T->Index = Query1->FieldCount;
T->DataSet = Query1;
Query1->FieldDefs->UpDate();
Query1->Open();
回复
gfh21cn 2002-08-01
跟TTable 一样的吧
用TBlobStream
回复
wyb_45 2002-08-01
读取和写入BLOB二进制数据,我比较拿手。
这里使用的是BDE连接。可以在大多数数据库里使用
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(!OpenDialog1->Execute())
return;
String SQL="insert into my_s (id_s,name_s,Binra_s) values ("+Edit1->Text+",'"+Edit2->Text+"',:Binra_s)";
TMemoryStream * s = new TMemoryStream;
s->LoadFromFile(OpenDialog1->FileName);
s->Position=0;

if(s->Size>1024*10240)
{
delete s;
ShowMessage("文件太大,无法保存");
return;
}
Query1->Close();
Query1->SQL->Clear();
Query1->SQL->Add(SQL);
// Query1->Params->ParamByName("id_s")->AsInteger=Edit1->Text.ToInt();
// Query1->Params->ParamByName("Bmp_s")->LoadFromStream(s,ftGraphic);
Query1->Params->ParamByName("Binra_s")->LoadFromStream(s,ftBlob);
try
{
Query1->ExecSQL();
ShowMessage("OK");
}
catch(...){ShowMessage("Err");}
delete s;
SpeedButton1->Click();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
String SQL="select * from my_s where id_s="+Edit3->Text;
Query1->Close();

Query1->SQL->Clear();
Query1->SQL->Add(SQL);
Query1->Open();
if(Query1->IsEmpty())
return;
try
{
TBlobStream * s=new TBlobStream ( (TBlobField *)Query1->FieldByName("Binra_s"),bmRead);
if(RadioGroup1->ItemIndex==0)
{
try
{
Image1->Picture->Bitmap->LoadFromStream(s);
}
catch(...){MessageDlg("图片加载错误,数据库内可能不是bmp图片", mtError, TMsgDlgButtons() << mbOK, 0);}
}
else
{
try
{
if(FileExists(Edit4->Text)) DeleteFile(Edit4->Text);
TFileStream *s1=new TFileStream(Edit4->Text,fmCreate|fmOpenWrite);
s1->CopyFrom(s,s->Size);
delete s1;
MessageDlg("保存文件成功", mtConfirmation, TMsgDlgButtons() << mbOK, 0);
}catch(...){MessageDlg("保存文件错误", mtError, TMsgDlgButtons() << mbOK, 0);}
}
delete s;


}
catch(...){MessageDlg("错误", mtError, TMsgDlgButtons() << mbOK, 0);}
/*
std::auto_ptr<TMemoryStream> stream (new TMemoryStream);
TBlobField* field = dynamic_cast<TBlobField *>(Query1->FieldByName("Binra_s"));
if (field)
{
field->SaveToStream(stream.get());
stream->Position = 0;
std::auto_ptr<TJPEGImage> jpeg(new TJPEGImage);
jpeg->LoadFromStream(stream.get());
if (jpeg->Height >0 && jpeg->Width >0)
Image1->Picture->Bitmap->Assign(jpeg.get());
else
Image1->Picture->Assign(NULL);
}
*/
}
//注意BDE的配置,否则,该字段保存的BLOB数据不完整。
回复
yangguo_god 2002-08-01
看看,学习
回复
runnerrunning 2002-08-01
如果是文本格式,可以直接用tdbmemo或者
直接付值:Query1->FieldByName("sdfs")->AsString = "sdfsa"
回复
runnerrunning 2002-08-01
TBlobField有两个函数:

LoadFromFile
LoadFromStream
都可以实现
回复
kingcaiyao 2002-08-01
可以用TMemoField,请看代码:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TQuery *Query=new TQuery(this);
Query->DatabaseName="your database name";
Query->RequestLive=true;
Query->Close();
Query->SQL->Clear();
Query->SQL->Add("select * from yourtable");
Query->Open();
Query->Edit();
TMemoField *pField=(TMemoField*)Query->FieldByName("your field name");
//如果该字段用于存储Text文本
TStringList *pList=new TStringList();
pList->Add(".....");
.....
pField->Assign(pField);
Query->Post();
//如果用于存储Image,可用TGraphicField
看看Assign方法的声明:
Copies a value to the BLOB field.

virtual void __fastcall Assign(Classes::TPersistent* Source);

Description

Use Assign to copy data to the BLOB field. Assign copies the Value of a BLOB field from

?Another BLOB field
?A TStrings object
?A TBitmap object, if the BlobType is ftGraphic or ftTypedBinary
?A TPicture object, if the BlobType is ftGraphic or ftTypedBinary
?Any object that supports TBlobField in its AssignTo method

In the example below, the Assign method is used to copy the bitmap from a TImage component into a BLOB field through its TBlobField field object.
//The rest of your code here
.....
}
回复
发动态
发帖子
数据库及相关技术
创建于2007-08-02

1156

社区成员

C++ Builder 数据库及相关技术
申请成为版主
社区公告
暂无公告