BCB保存图片到SQLSERVER

void __fastcall TForm1::btn6Click(TObject *Sender)   //存储图片
{
      TMemoryStream * MemoryImageStream=new TMemoryStream;                        //A
      img1->Picture->Graphic->SaveToStream(MemoryImageStream);                    //B


      AnsiString strSql = "insert into tab_img(Img) values(:Img)";
      qry1->Close();
      qry1->SQL->Clear();
      qry1->SQL->Add(strSql);
      TJPEGImage *pJPG = new TJPEGImage;
      pJPG->Assign(img1->Picture->Graphic);
      pJPG->SaveToStream(MemoryImageStream);


      MemoryImageStream->Position = 0;
      this->qry1->Parameters->ParamByName("Img")->LoadFromStream(MemoryImageStream,ftBlob);
      qry1->ExecSQL();






                              //E
       delete MemoryImageStream;
       delete pJPG;


}
//---------------------------------------------------------------------------


void __fastcall TForm1::btn7Click(TObject *Sender)  //加载图片
{
      TMemoryStream * MemoryImageStream=new TMemoryStream;                        //A
      img1->Picture->Graphic->SaveToStream(MemoryImageStream);                    //B


      AnsiString strSql = "select top 1 * from tab_img";
      qry1->Close();
      qry1->SQL->Clear();
      qry1->SQL->Add(strSql);
      qry1->Open();
      if(!qry1->Eof)
      {
        if(qry1->FieldByName("Img"))
        {
            ((TBlobField*)(qry1->FieldByName("Img")))->SaveToStream(MemoryImageStream);


            TJPEGImage *pJPG = new TJPEGImage;
            MemoryImageStream->Position = 0;
            pJPG->LoadFromStream(MemoryImageStream);
            img2->Picture->Assign(pJPG);


            if(MemoryImageStream != NULL)
            {
               delete MemoryImageStream;
               MemoryImageStream = NULL;
            }
            if(pJPG != NULL)
            {
               delete pJPG;
              pJPG = NULL;
            }
        }
        else
        img2->Picture = NULL;
      }
}
//---------------------------------------------------------------------------

你可能感兴趣的:(BCB保存图片到SQLSERVER)