首页 > 代码库 > 数据库的OLE字段写入长二进制文件

数据库的OLE字段写入长二进制文件

//‘***************************************************************************************
//‘函数:将数据库的OLE字段写入长二进制文件
//‘***************************************************************************************
//bool THsPart_clsPartCommonDB::PrintFieldToFile(TheFiled As ADODB.Field, ByVal strSaveName As String, _
// Optional IsAppend As Boolean)
bool THsPart_clsPartEntityDB::PrintFieldToFile(_RecordsetPtr &myRcdPtr,_variant_t index ,CString strSaveName /*ADODB.Fields TheFiled, CString strSaveName ,*Optional IsAppend As Boolean*/)
{
    //Dim MyRc As Recordset;
 VARIANT varBLOB;
 CFile myfile;
 bool bReturn = false;
    try
 {
   
 
  //‘定义各种参数
  const int conChunkSize = 16384;
  int Chunks ;
  byte *Chunk = NULL; //Chunk() As Byte,
  //int FileNumber;
  long FragMent ,lngTotalSize;
  // 定义各种参数
  myfile.Open(strSaveName,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary);
  /*FileNumber = FreeFile;
  Open strSaveName For Binary Access Write As #FileNumber;*/
 
  //将字段内的长二进制文件导出的文件
  lngTotalSize = myRcdPtr->Fields->GetItem(index)->ActualSize; //lngTotalSize = TheFiled.ActualSize;
  Chunks = lngTotalSize /conChunkSize;
  FragMent = lngTotalSize % conChunkSize;
  Chunk = (byte *)GlobalAlloc(GMEM_FIXED,FragMent); //ReDim Chunk(FragMent);
  varBLOB= myRcdPtr->Fields->GetItem(index)->GetChunk(FragMent); //Chunk() = TheFiled.GetChunk(FragMent);
  SafeArrayAccessData(varBLOB.parray,(void **)&Chunk);
  LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)Chunk);
  myfile.Write(buffer,FragMent);
  GlobalUnlock((HGLOBAL)Chunk);
  SafeArrayUnaccessData (varBLOB.parray);
  int counter;
  Chunk = (byte *)GlobalAlloc(GMEM_FIXED,conChunkSize);
  for(counter=1;counter<= Chunks;counter++) //For counter = 1 To Chunks
  {
   //Chunk() = TheFiled.GetChunk(conChunkSize);
   varBLOB= myRcdPtr->Fields->GetItem(index)->GetChunk(conChunkSize);
   SafeArrayAccessData(varBLOB.parray,(void **)&Chunk);
   buffer = (LPSTR)GlobalLock((HGLOBAL)Chunk);
   myfile.Write(Chunk,FragMent);
   SafeArrayUnaccessData (varBLOB.parray);
  }
  GlobalUnlock((HGLOBAL)Chunk);
  Chunk = NULL;
  buffer = NULL;
  ////////////////////////////////////////////////
 
  ////////////////////////////
 
  //////////
  //Close #FileNumber
  myfile.Close();
 
  bReturn = true;
  return bReturn;
 }
 catch(...)
 {
 
  AfxMessageBox(_T("PrintFieldToFile wrong!"));
  //Close #FileNumber;
  myfile.Close();
 }
 return bReturn;
}

数据库的OLE字段写入长二进制文件