SQLSERVER图像存取的问题?(200分)

  • 主题发起人 主题发起人 wqhatnet
  • 开始时间 开始时间
W

wqhatnet

Unregistered / Unconfirmed
GUEST, unregistred user!
1、SQLSERVER中应该设置什么字段?<br>2、存的代码?<br>3、取的代码?
 
一、字段的类型设置为:Image<br>二、保存到数据库里(存的代码):<br>var<br>&nbsp; Stream: TMemoryStream;<br>begin<br>&nbsp; if OpenDialog1.Execute then<br>&nbsp; begin<br>&nbsp; &nbsp; ADOTable1.Append;<br>&nbsp; &nbsp; ADOTable1.FieldByName('Filename').AsString := ExtractFileName(OpenDialog1.FileName);<br>&nbsp; &nbsp; Stream := TMemoryStream.Create;<br>&nbsp; &nbsp; Stream.Position := 0;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; Stream.LoadFromFile(OpenDialog1.FileName);<br>&nbsp; &nbsp; &nbsp; TBlobField(ADOTable1.FieldByName('FileBode')).LoadFromStream(Stream);<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; Stream.Free;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>三、从数据库取保存到文件(取的代码):<br>var<br>&nbsp; Stream: TMemoryStream;<br>&nbsp; Conn: string;<br>begin<br>&nbsp; if SaveDialog1.Execute then<br>&nbsp; begin<br>&nbsp; &nbsp; Conn := SaveDialog1.FileName;<br>&nbsp; &nbsp; Stream := TMemoryStream.Create;<br>&nbsp; &nbsp; Stream.Position := 0;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; TBlobField(ADOTable1.FieldByName('FileBode')).SaveToStream(Stream);<br>&nbsp; &nbsp; &nbsp; Stream.SaveToFile(Conn);<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; Stream.Free;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;
 
字段的类型设置为:Image<br>存:(FieldByName('img') as TBlobField).LoadFromFile(ExplainFileName)<br>取:(FieldByName('img') as TBlobField).SaveToStream(stream);<br>&nbsp; &nbsp; &nbsp;img.Picture.Graphic.LoadFromStream(stream);
 
存图片路径。。。
 
我不想保存到文件而是直接显示到IMAGE中
 
用 img.Picture.Graphic.LoadFromStream(stream);<br>出错
 
我用了二进制<br>IMAGE的好象只能存取BMP图片
 
Invalid calss typecast<br><br>怎么出现了这个错误?
 
类型转换错误,试试这个:<br>uses jpeg;<br>procedure GetImaeg(DataSet: TDataSet);<br>var<br>&nbsp; TempStream: TStringStream;<br>&nbsp; TempJpeg: TJPEGImage;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; TempStream := nil;<br>&nbsp; &nbsp; TempJpeg := nil;<br>&nbsp; &nbsp; TempStream:=TStringStream.Create('');<br>&nbsp; &nbsp; TempJpeg:=TJPEGImage.Create;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; if DataSet.FieldByName('CLTP0').IsNull then Exit;<br><br>&nbsp; &nbsp; &nbsp; TBlobField(DataSet.FieldByName('CLTP0')).SaveToStream(tempstream);<br>&nbsp; &nbsp; &nbsp; TempStream.Position:=0;<br>&nbsp; &nbsp; &nbsp; TempJpeg.LoadFromStream(TempStream);<br>&nbsp; &nbsp; &nbsp; Image1.Picture.Assign(TempJpeg); &nbsp; <br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; on e:exception do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Application.MessageBox('图片读取失败!','提示',MB_ICONERROR);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; TempStream.Free;<br>&nbsp; &nbsp; TempJpeg.Free;<br>&nbsp; end;<br>end;<br>//存图片<br>proceudre SaveTODB(DataSet: TADOQuery);<br>var<br>&nbsp; TMStream:TMemoryStream; <br>begin<br>&nbsp; try<br>&nbsp; &nbsp; DataSet.Post;<br>&nbsp; &nbsp; &nbsp;if OpenPictureDialog1.FileName&lt;&gt;'' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TMStream:=TMemoryStream.Create;//创建内存流<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TMStream.LoadFromFile(OpenPictureDialog1.FileName);//转换OpenPictureDialog1.FileName为内存流,从文件导入<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataSet.Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataSet.SQL.Clear;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataSet.SQL.Add('update TableName set photo=:ID where id='+#39+trim(edit1.Text)+#39);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataSet.Parameters.ParamByName('ID').LoadFromStream(TMStream,ftTypedBinary);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataSet.ExecSQL;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TMStream.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; showmessage('数据存储成功!');<br>&nbsp; except<br>&nbsp; &nbsp; on e:exception do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; application.MessageBox('数据存储失败!','提示',MB_ICONERROR);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;end;<br>end;
 
多人接受答案了。
 
后退
顶部