如何在MSSQL中使用image字段进行jpg图片存取?

如何在MSSQL中使用image字段进行jpg图片存取?

//===========================================================================
// 数据库结构如下
//---------------------------------------------------------------------------
// if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Tbl_RealTimeDataInterFace]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
// drop table [dbo].[Tbl_RealTimeDataInterFace]
// GO
//
// CREATE TABLE [dbo].[Tbl_RealTimeDataInterFace] (
// [Id] [int] IDENTITY (1, 1) NOT NULL ,
// [WheelNum] [tinyint] NULL ,
// [PictureName] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
// [Picture] [image] NULL ,
// ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
// GO
//---------------------------------------------------------------------------

//===========================================================================
// 参考网站
//---------------------------------------------------------------------------
// http://community.csdn.net/Expert/topic/4817/4817560.xml?temp=.8010218
//---------------------------------------------------------------------------

/*
*===========================================================================
* AuthorFeng Weizhao
* Date 2006/12/15
*
* 功能: BCB 6 环境下SQL数据库中jpg图片存入,显示
*
*===========================================================================
*/

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
/* ok
AnsiString Sql = "select top 1 * from Tbl_RealTimeDataInterFace order by id desc ";

ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = Sql;
ADOQuery1->Open();

// ADOQuery1->Append();
TBlobField *blob=(TBlobField *)ADOQuery1->FieldByName("Picture");
blob->LoadFromFile("StillCap001.jpg");
ADOQuery1->Post();
*/

try{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = "insert into Tbl_RealTimeDataInterFace (WheelNum,Picture) values (2,:picture)";
ADOQuery1->Parameters->ParamByName("picture")->LoadFromFile("StillCap001.jpg",ftBlob);
ADOQuery1->ExecSQL();
}catch(...){
ShowMessage("写入数据库错误");
}
/* not ok
AnsiString Sql = "select top 1 * from Tbl_RealTimeDataInterFace order by id desc";

ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = Sql;
ADOQuery1->Open();

ADOQuery1->Edit();
TBlobField *pField = (TBlobField *)ADOQuery1->Parameters->ParamByName("Picture");
TBlobStream *pmem = new TBlobStream(pField, bmWrite);
pmem->Seek(0,soFromBeginning);
pField->SaveToStream(pmem);

Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
pBitmap->Assign(Image1->Picture->Graphic);
pBitmap->SaveToStream(pmem);
ADOQuery1->Post();
*/
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
try{ // 不成功
AnsiString Sql = "select top 1 * from Tbl_RealTimeDataInterFace order by [id] desc ";

ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = Sql;
ADOQuery1->Open();

TMemoryStream *Stream = new TMemoryStream();
Stream->Seek(0,soFromBeginning);
TBlobField *blob = (TBlobField *)ADOQuery1->FieldByName("Picture");
blob->SaveToStream(Stream);

Image2->Picture->Bitmap->LoadFromStream(Stream);
}catch(...){}
}
//---------------------- 通过文件读出 jpg 文件-----------------------------------------------------
void __fastcall TForm1::btn_1Click(TObject *Sender)
{
AnsiString Sql = "select top 1 * from Tbl_RealTimeDataInterFace order by [id] desc ";

ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = Sql;
ADOQuery1->Open();

TBlobField *blob = (TBlobField *)ADOQuery1->FieldByName("Picture");
blob->SaveToFile("aa.jpg");//或者SaveToStream(xxx);
Image2->Picture->LoadFromFile("aa.jpg");//或者LoadFromStream(xxx);
}
//---------------------------- 直接从内存中读出jpg文件 ----------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
AnsiString Sql = "select top 1 * from Tbl_RealTimeDataInterFace order by [id] desc ";

ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = Sql;
ADOQuery1->Open();

TMemoryStream *MemoryStream = new TMemoryStream();
TJPEGImage *JpgImage = new TJPEGImage();
try
{
dynamic_cast<TBlobField*>(ADOQuery1->FieldByName("Picture"))->SaveToStream(MemoryStream);
MemoryStream->Position=0;
JpgImage->LoadFromStream(MemoryStream);
Image2->Picture->Assign(JpgImage);
}catch(...)
{
delete MemoryStream;
delete JpgImage;
}
delete MemoryStream;
delete JpgImage;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
AnsiString Sql = "select * from Tbl_RealTimeDataInterFace order by [id] desc ";

ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Text = Sql;
ADOQuery1->Open();

TStream *Stream1;
TJPEGImage *Pjp = new TJPEGImage();

try
{
Stream1=ADOQuery1->CreateBlobStream(ADOQuery1->FieldByName("Picture"), bmRead); //Picture是存放jpeg内容的字段,它的类型一定要用image
Pjp->LoadFromStream(Stream1);

dbimg_1->Picture->Bitmap->Assign(Pjp); // dbimg_1是TDBImage组件,它的 DateSource,和 FieldName属性要空着

Image2->Picture->Bitmap->Assign(Pjp);
delete Stream1;
}
__finally
{
ADOQuery1->Close();
delete Pjp;
}
}
//------------------------- 清空图片 --------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Image2->Picture->Assign(NULL);
}