如何把任意文件存入数据库问题

Redball 2003-08-29 08:31:16
文件都是二进制的,内容存入一个单元,文件名存入一个单元,不就实现了文件在数据库中的存放了么。
这个思想具体怎么实现阿?
...全文
28 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzhong2 2003-08-29
  • 打赏
  • 举报
回复
数据类型一定要是image
以下是以存图片为例,实际应用中,读什么文件存什么文件
存jpeg到SQL数据库:
if(OpenPictureDialog1->Execute())
{
ADOQuery1->Edit();
TBlobField *Field = (TBlobField*)ADOQuery1->FieldByName("photo");
Field->LoadFromFile(OpenPictureDialog1->FileName);
ADOQuery1->Post();
}

//以下是从数据库读jpeg,并显示在TDBImage控件中
#include <clipbrd.hpp>

TStream *Stream1;
TJPEGImage *Pjp;

Pjp=new TJPEGImage();

ADOQuery1->Open();
try
{
Stream1=ADOQuery1->CreateBlobStream(ADOQuery1->FieldByName("treenodes"), bmRead);//treenodes是存放jpeg内容的字段,它的类型一定要用image
Pjp->LoadFromStream(Stream1);
//Image2是TDBImage组件,它的DateSource,和FieldName属性要空着
Image2->Picture->Bitmap->Assign(Pjp);
delete Stream1;
}
__finally
{
ADOQuery1->Close();
delete Pjp;
}


下面是别人的更好的方法,可存各种图形
以下是讀出各種類型的圖片的程序,支持ADO,BDE或TClientDataSet
#define PICTURE_MAP__(TBit) {TBit *PG = new TBit(); \
try {PG->LoadFromStream(TmpStream);\
Pic->Assign(PG); \
}\
catch(...)\
{delete PG ;\
return false;\
}\
delete PG;\
}
//----------------------------------------------------------------
//該模板將二進制字段中的圖像(GIF或JPG等等)使用Assign方法轉為TPicture,TBitmap等等。
template <class T >
bool LoadPhotoFromField(TField *F_Photo,const AnsiString Format,T *Pic)
{if(!F_Photo->DataSet->Active) return false ;
if(F_Photo->IsNull) return false ;
else
{TStream *TmpStream = F_Photo->DataSet->CreateBlobStream(F_Photo,bmRead);
if(Format == ".JPG" || Format == ".JPEG")PICTURE_MAP__(TJPEGImage )
else if(Format == ".BMP") PICTURE_MAP__(Graphics::TBitmap)
// else if(Format == ".GIF") PICTURE_MAP__(TGIFImage )
else if(Format == ".ICO") PICTURE_MAP__(TIcon)
else if(Format == ".WMF" || Format ==".EMF") PICTURE_MAP__(TMetafile)
else return false ;
}
return true;
}
#undef PICTURE_MAP__(TBit)
//如果要支持GIF,那你要安裝支持GIF的VCL類。



支持多種格式
存入:
if(OpenPictureDialog1->Execute())
{DataSet->Edit();
TBlobField *Field = (TBlobField*)DataSet->FieldByName("photo");
Field->LoadFromFile(OpenPictureDialog1->FileName);
DataSet->FieldByName("photoFormat")->AsString =
ExtractFileExt(OpenPictureDialog1->FileName).UpperCase();
DataSet->Post();
}

Robin 2003-08-29
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
ADOTable1->Insert();
if(OpenPictureDialog1->Execute())
{
Edit1->Text=OpenPictureDialog1->FileName;
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
TMemoryStream *stream = new TMemoryStream();
Image1->Picture->Bitmap->SaveToStream(stream);
ADOTable1->Insert();
ADOTable1->FieldByName("title")->AsString=Edit1->Text;
((TGraphicField *)(ADOTable1->FieldByName("content")))->LoadFromStream(stream);
ADOTable1->Post();
delete stream;

}
//---------------------------------------------------------------------------
Robin 2003-08-29
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Dialogs.hpp>
#include <ExtDlgs.hpp>
#include <ADODB.hpp>
#include <DB.hpp>
#include <DBCtrls.hpp>
#include <Mask.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TOpenPictureDialog *OpenPictureDialog1;
TADOTable *ADOTable1;
TWideStringField *ADOTable1title;
TBlobField *ADOTable1content;
TEdit *Edit1;
TImage *Image1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Robin 2003-08-29
  • 打赏
  • 举报
回复
回答的积极也有问题啊!
Redball 2003-08-29
  • 打赏
  • 举报
回复
flyinger(琉璃翡翠) :怎么我的贴你回的最积极阿
我就是问怎么实现,代码~!代码~!!或伪码也行。
binbin 2003-08-29
  • 打赏
  • 举报
回复
没什么问题,用blob字段就可以了.
用流来和blob字段读写内容.
Robin 2003-08-29
  • 打赏
  • 举报
回复
大哥!
可以啊!
怎么了,有问题吗!

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧