大家帮忙解决SQL问题,急!急!(100分)

  • 主题发起人 主题发起人 黑鹰船长
  • 开始时间 开始时间

黑鹰船长

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样用SQL语句想数据库写image类型的数据及怎样去读写,更新
 
好像不能直接用sql实现,
用传参数的方法可行。
 
最好把image类型的数据转换成Base64
 
数据库SQL Server,存放文件的字段类型Image
Create Table
MyTable(FileName VarChar(20),FileSource Image)
存放文件到数据库var FileName :String;
FileName := ExtractFileName(OpenDialog1.FileName);
with Query do begin
Close;
Sql.Clear;
Sql.Add('INSERT INTO MyTable VALUES (:FileName,:FileSource)');
ParamByName('FileName').AsString := FileName;
ParamByName('FileSource').LoadFromFile(OpenDialog1.FileName,ftBolob);
ExecSQL;
end;
从数据库中取出文件var FileName :String;
begin with Query do
begin
Close;
Sql.Clear;
Sql.Add('SELECT * FROM MyTable WHERE FileName = '?'');
Open; FileName := 'c:/'+FieldByName('FileName').AsString;
(FieldByName('FileSource') AS TBlobField).SaveToFile(FileName);
end;
end;
 
存(sql server):
function TInputF.SavePic(cID: String; cImage: TImage): SmallInt;
var
JPEG: TJPEGImage;
ImageStream: TMemoryStream;
begin
Result := -1;
if cImage.Picture.Graphic = nil then
Exit;
if not DBConnect(DataM.ADOCo) then
Exit;
JPEG := TJPEGImage.Create;
ImageStream := TMemoryStream.Create;
try
JPEG.Scale := jsEighth;
JPEG.CompressionQuality := 100;
JPEG.Assign(cImage.Picture.Graphic);
JPEG.SaveToStream(ImageStream);
with DataM do
begin
try
ADOQryImage.Close;
ADOQryImage.SQL.Text := 'insert into T_IMAGE (ID, Photo) values (:ID, :Photo)';
ADOQryImage.Parameters.ParamByName('ID').Value := cID;
ADOQryImage.Parameters.ParamByName('Photo').LoadFromStream(ImageStream, ftBlob);
ADOQryImage.ExecSQL;
Result := 1;
except
Result := 0;
ADOCo.Connected := false;
end;
end;
finally
JPEG.Free;
ImageStream.Free;
end;
end;
 
取:
function TInputF.LoadPic(cID: String; cImage: TImage): SmallInt;
var
ImageStream: TMemoryStream;
JPEG: TJPEGImage;
begin
Result := -1;
cImage.Picture.Assign(nil);
if not DBConnect(DataM.ADOCo) then
Exit;
with DataM do
begin
ImageStream := TMemoryStream.Create;
JPEG := TJPEGImage.Create;
ADOQryImage.Close;
ADOQryImage.SQL.Text := 'select * from T_IMAGE where ID = :ID';
ADOQryImage.Parameters.ParamByName('ID').Value := cID;
try
ADOQryImage.Open;
if not ADOQryImage.IsEmpty then
begin
TBlobField(ADOQryImage.FieldByName('Photo')).SaveToStream(ImageStream);
ImageStream.Position := 0;
JPEG.LoadFromStream(ImageStream);
cImage.Picture.Assign(JPEG);
end;
Result := 1;
except
Result := 0;
ADOCo.Connected := false;
end;
FreeAndNil(JPEG);
ImageStream.Free;
end;
end;
 
好我先去看看,行的话我一定给分,谢谢
另外,如果我的图片是在 image或 DBImage控件中,是否用同样的方法可以实现?
 
procedure TForm1.Button1Click(Sender: TObject);
var
C: TClipboard;
begin
C := TClipboard.Create;
try
if Clipboard.HasFormat(CF_BITMAP) then

DBImage1.PasteFromClipboard
else
ShowMessage('Clipboard does not contain a bitmap!');
finally
C.Free;
end;
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
Table1Bitmap.LoadFromFile(
'c:/delphi/images/splash/16color/construc.bmp');
end;

procedure TForm1.Button3Click(Sender: TObject);
var
B: TBitmap;
begin
B := TBitmap.Create;
try
B.LoadFromFile('c:/delphi/images/splash/16color/athena.bmp');
DBImage1.Picture.Assign(B);
finally
B.Free;
end;
end;
///////////////////////////////////////////////////////////
var
st: TStringStream;
begin
st := tstringstream.create('');
bitmap.savetostream(st);
query1.sql.text := 'insert into table Image_field values :bmp';
query1.parambyname('bmp').asblob := st.datastring;
query1.execsql;
st.free;
end;
////////////////////////////////////////////////////////////////////////
unit Unit1;

interface

{$IFDEF WIN32}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, Db,
DBTables;
{$ELSE}
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, DBTables, DB, Grids, DBGrids, ExtCtrls, StdCtrls;
{$ENDIF}

type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Image1: TImage;
Button1: TButton;
Table1Name: TStringField;
Table1WMF: TBlobField;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure DataSource1DataChange(Sender: TObject; Field: TField);
private
{ Private declarations }
FileName : string; {Used to hold a temp file name}
procedure LoadWMFFromDatabase; {loads a WMF from the database}
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
{Used for loading metafiles}
OpenDialog1.Filter := 'Metafiles (*.wmf)|*.wmf';
OpenDialog1.Options := [ofHideReadOnly, ofNoChangeDir];
Image1.Stretch := true;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
{Erase the temp file if it exists}
if FileName <> '' then
DeleteFile(FileName);
end;

{This function gets a temporary file name form the system}
function GetTemporaryFileName : string;
{$IFNDEF WIN32}
const MAX_PATH = 144;
{$ENDIF}
var
{$IFDEF WIN32}
lpPathBuffer : PChar;
{$ENDIF}
lpbuffer : PChar;
begin
{Get the file name buffer}
GetMem(lpBuffer, MAX_PATH);
{$IFDEF WIN32}
{Get the temp path buffer}
GetMem(lpPathBuffer, MAX_PATH);
{Get the temp path}
GetTempPath(MAX_PATH, lpPathBuffer);
{Get the temp file name}
GetTempFileName(lpPathBuffer,
'tmp',
0,
lpBuffer);
{Free the temp path buffer}
FreeMem(lpPathBuffer, MAX_PATH);
{$ELSE}
{Get the temp file name}
GetTempFileName(GetTempDrive('C'),
'tmp',
0,
lpBuffer);
{$ENDIF}
{Create a pascal string containg}
{the temp file name and return it}
result := StrPas(lpBuffer);
{Free the file name buffer}
FreeMem(lpBuffer, MAX_PATH);
end;

procedure TForm1.LoadWMFFromDatabase;
var
FileStream: TFileStream; {a temp file}
BlobStream: TBlobStream; {the WMF Blob}
begin
Image1.Picture.Metafile.Assign(nil);
{Create a blob stream for the WMF blob}
BlobStream := TBlobStream.Create(Table1WMF, bmRead);
if BlobStream.Size = 0 then begin
BlobStream.Free;
Exit;
end;
{if we have a temp file then erase it}
if FileName <> '' then
DeleteFile(FileName);
{Get a temp file name}
FileName := GetTemporaryFileName;
{Create a temp file stream}
FileStream := TFileStream.Create(FileName,
fmCreate or fmOpenWrite);
{Copy the blob to the temp file}
FileStream.CopyFrom(BlobStream, BlobStream.Size);
{Free the streams}
FileStream.Free;
BlobStream.Free;
{Dispaly the image}
Image1.Picture.Metafile.LoadFromFile(FileName);
end;

{Save a wmf file to the database}
procedure TForm1.Button1Click(Sender: TObject);
var
FileStream: TFileStream; {to load the wmf file}
BlobStream: TBlobStream; {to save to the blob}
begin
{Allow the button to repaint}
Application.ProcessMessages;
if OpenDialog1.Execute then begin
{Turn off the button}
Button1.Enabled := false;
{Assign the avi file name to read}
FileStream := TFileStream.Create(OpenDialog1.FileName,
fmOpenRead);
Table1.Edit;
{Create a BlobStream for the field Table1WMF}
BlobStream := TBlobStream.Create(Table1WMF, bmReadWrite);
{Seek to the Begginning of the stream}
BlobStream.Seek(0, soFromBeginning);
{Delete any data that may be there}
BlobStream.Truncate;
{Copy from the FileStream to the BlobStream}
BlobStream.CopyFrom(FileStream, FileStream.Size);
{Free the streams}
FileStream.Free;
BlobStream.Free;
{Post the record}
Table1.Post;
{Load the metafile in to a TImage}
LoadWMFFromDatabase;
{Enable the button}
Button1.Enabled := true;
end;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
if (Sender as TDataSource).State = dsBrowse then
LoadWMFFromDatabase;
end;

end.
 
>>另外,如果我的图片是在 image或 DBImage控件中,是否用同样的方法可以实现?
你只要把image作为参数传入我给你的函数即可。
 
接受答案了.
 
有没搞错
up=100?
 

Similar threads

回复
0
查看
1K
不得闲
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
916
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部