Invalid class typecast出现这个错误,求解决方法?
sql server200:
字段“模板”字段类型“binary”
----------------------------------------
---运行下段代码
Table1->Edit();
((TBlobField *)(Table1->FieldByName("模板")))->LoadFromFile("c:\\aaa.txt");
Table1->Post();
---出显以下错误提示:
Invalid class typecast
--求解决方法
[解决办法]
代码没有问题,Binary字段并不是被转换成TBlobField,而是TBytesField或者TBinaryField,所以不能转换。Image数据类型才对应TBlobField
[解决办法]
SQL Server 2000 中存取文件是用二进制的方式保存到数据库中。SQL2000版本提供image字段类型的。
//用流的方式存入图片到数据库void __fastcall TForm1::BitBtn1Click(TObject *Sender){ TMemoryStream *pMS=new TMemoryStream; //创建一个流 pMS->LoadFromFile(Edit1->Text);// 选择一个文件,读入流 pMS->Seek(0,soFromBeginning);//设置流指针0 ADOQuery1->SQL->Clear(); ADOQuery1->SQL->Add("insert into Pimage (name,image) values (:title,:image)"); ADOQuery1->Parameters->ParamByName("title")->Value="asdf"; ADOQuery1->Parameters->ParamByName("image")->LoadFromStream(pMS,ftBlob); try { ADOQuery1->ExecSQL();//执行SQL } catch(Exception &exception) { Application->ShowException(&exception); } delete pMS; pMS=NULL;}void __fastcall TForm1::BitBtn3Click(TObject *Sender){ /* //读图片在窗体上显示 ADOQuery2->Open(); TBlobField* pField=(TBlobField*)ADOQuery2->FieldByName("image"); TADOBlobStream* pmem=new TADOBlobStream(pField,bmRead); pmem->Seek(0,soFromBeginning);//初始化流指针 Graphics::TBitmap* pBitmap=new Graphics::TBitmap(); pBitmap->LoadFromStream(pmem); // Image1->Assign(pBitmap); delete pmem; delete pBitmap; *//**/if(SaveDialog1->Execute()) { //读图片并写入文件 ADOQuery2->Open(); TMemoryStream *pMS=new TMemoryStream; TBlobField* pField = (TBlobField*)ADOQuery2->FieldByName("image"); pField->AsVariant pMS->Seek(0,soFromBeginning); pMS->Size=0; pField->SaveToStream(pMS); pMS->SaveToFile(SaveDialog1->FileName); } //}
[解决办法]
使用Image数据类型