文件读写问题
我声明了 unsigned char buf[20480],在程序中给buf赋值了,想把它插入到数据库的table表中,表中字段的类型是image,该怎么写呢?
我的思路是:把值写入到二进制的文件中,再保存到数据库,但我不知道该怎么写? 哪位大侠知道呢?
或者有更好的方法吧buf的值插入到image类型的字段中
[解决办法]
SQL Server2000 我用的image 类型 //存储 TBlobField *blob_pic=(TBlobField *)UserMod->TInsert->FieldByName("IMAGE"); try { blob_pic->LoadFromFile(app_path+"\\temp\\temp.jpg"); } catch(...) { ShowMessage("图像存盘失败!"); } UserMod->TInsert->Post(); UserMod->TInsert->Refresh(); } //读取 TBlobField *blob_pic=(TBlobField *)DBGrid4->DataSource->DataSet->FieldByName("IMAGE"); try { blob_pic->SaveToFile(app_path+"\\temp\\temp1.jpg"); Image9->Picture->LoadFromFile(app_path+"\\temp\\temp1.jpg"); } catch(...) { ShowMessage("图像读取失败!"); } delete blob_pic;//--------struct数据存取---------------------------------------//结构体定义:typedef struct { unsigned short Weight; unsigned short Dis; char Type; char Speed; }VehAxle;VehAxle Axle[15],PrintAxle[15];//---------------------------------//数据写入 C/C++ code TBlobField *BlobField=(TBlobField *) DataMod->InsertTable->FieldByName("Veh_Detail"); //BlobField->AsString=*sp; //delete sp; FILE *fp = fopen("save.dat","wb"); fwrite(&Axle,sizeof(VehAxle)*15,1,fp); //fread(&Axle,sizeof(VehAxle)*15,8,fp); fclose(fp); try { BlobField->LoadFromFile("save.dat"); } catch(...) { }//数据读取C/C++ code TBlobField *blob_file=(TBlobField *)Veh_DBGrid->DataSource->DataSet->FieldByName("Veh_Detail"); try { blob_file->SaveToFile("read.dat"); Sleep(10); FILE *fp=fopen("read.dat","r+b"); fread(&PrintAxle,sizeof(VehAxle)*15,1,fp); fclose(fp); } catch(...) { // }本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xjq2003/archive/2009/02/17/3898634.aspx
[解决办法]
我以前做过图片存取,用SQL 2000,在数据里,图片是以二进制形式存的。那时遇到一个问题,就没办法插入单个数据。只能,删除原有的行,重新插入这个行,插入同时把图片一起存进去。单个插入图片,不行。
[解决办法]
SQL SERVER 2000数据库
表TABLE1 字段imageid int型,image image型,自增字段
BCB6.0 TADOConnection,TADOQuery
//读写imageid=1记录的image字段数据
//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit1.h"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;unsigned char buf[20480];//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ //填充buf... ... //写入image字段 ADOQuery1->Close(); ADOQuery1->SQL->Text="SELECT image FROM TABLE1 WHERE imageid=1"; ADOQuery1->Open(); ADOQuery1->Edit(); TMemoryStream *S=new TMemoryStream; S->SetSize(20480); S->Clear(); S->Position=0; S->Write(buf,20480); TBlobField *T=(TBlobField*)(ADOQuery1->FieldByName("image")); T->Clear(); T->LoadFromStream(S); ADOQuery1->Post(); ADOQuery1->Close(); delete S;}//---------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender){ ZeroMemory(buf,sizeof(buf));//清零 //读取image字段 ADOQuery1->Close(); ADOQuery1->SQL->Text="SELECT image FROM TABLE1 WHERE imageid=1"; ADOQuery1->Open(); TMemoryStream *S=new TMemoryStream; S->SetSize(20480); S->Clear(); S->Position=0; TBlobField *T=(TBlobField*)(ADOQuery1->FieldByName("image")); T->SaveToStream(S); S->Position=0; S->Read(buf,20480);//填充buf ADOQuery1->Close(); delete S;}//---------------------------------------