下面通过详细的例子来讲述如何在delphi中处理sql server中的p_w_picpath、text字段。因为实际开发的需要, 我们需要处理的是text类型的字段,包括读和写。网上很多相关的文章都是讲述的对p_w_picpath的读写操作,下面首先介绍如何将图象存储在sql server的p_w_picpath字段。
其中 DataMConn为一个针对数据库操作的单元,放置一个ADOConnection,一个ADOQuery
//p_w_picpath to database
procedure TfrmText.Button1Click(Sender: TObject);
var
bm:tbitmap;
ms:TMemoryStream;
begin
ms:=TMemoryStream.Create;
bm:=TBitmap.Create;
bm.Assign(p_w_picpath1.Picture.Bitmap);
bm.SaveToStream(ms);
with DataMConn.ADOQHistory do
begin
Close;
SQL.Clear;
SQL.Add('INSERT INTO Package(PackageID,TempPackage) VALUES(:x,:y)');
Parameters.ParamByName('y').LoadFromStream(ms,ftBlob);
Parameters.ParamByName('x').Value := 'aaaaa';
ExecSQL;
end;
end;
//show p_w_picpath
procedure TfrmText.Button2Click(Sender: TObject);
var
Stream:TStream;
bm:tbitmap;
begin
with DataMConn.ADOQHistory do
begin
Close;
SQL.Clear;
SQL.Add('SELECT * FROM Package WHERE packageID= ''aaaaa''');
Try
Open;
stream := DataMConn.ADOQHistory.CreateBlobStream(FieldByName('TempPackage'),bmRead);
bm:=TBitmap.Create;
bm.LoadFromStream(stream);
p_w_picpath2.Picture.bitmap.Assign(bm);
stream.Free;
except
begin
ShowMessage('Error!');
Exit;
end;
end;//try
end;
end;
下面的两个例子是如何处理text类型的字段,其中读取的时候,利用了一个TDBMemo控件,来加载读取的流数据,然后赋值给一个WideString类型的变量str
//text to stream
procedure TfrmText.Button3Click(Sender: TObject);
var
str : WideString;
ss:TStringStream;
i : integer;
begin
str := 'sstrstrststrstrststrstrstrsttrstrstrrstrstr';
for i := 1 to 10000 do
begin
str := str + 'sstrstrststrstrststrstrstrsttrstrstrrstrstr'; //43万多个字节
end;
str := str + 'E';
ss := TStringStream.Create(str);
//bm:=TBitmap.Create;
//bm.Assign(p_w_picpath1.Picture.Bitmap);
//bm.SaveToStream(ms);
with DataMConn.ADOQHistory do
begin
Close;
SQL.Clear;
SQL.Add('INSERT INTO tPackage(PackageID,TempPackage) VALUES(:x,:y)');
Parameters.ParamByName('y').LoadFromStream(ss,ftMemo);
Parameters.ParamByName('x').Value := 'aaaaa';
ExecSQL;
end;
end;
//read text to a TDBMemo
procedure TfrmText.Button4Click(Sender: TObject);
var
stream : TStream;
str : WideString;
begin
with DataMConn.ADOQHistory do
begin
Close;
SQL.Clear;
SQL.Add('SELECT TempPackage FROM tPackage WHERE packageID= ''aaaaa''');
Open;
if not IsEmpty then
begin
Stream := DataMConn.ADOQHistory.CreateBlobStream(FieldByName('TempPackage'),bmRead);
stream.Position := 0;
AMemo.Lines.LoadFromStream(stream);
stream.Free;
end;
end;
end;
//下面是把text字段的数据内容直接读到一个WideString中,而不通过TDBMemo,因为在实际中,不知道何种原因,线程读到数据后,只能在第一次处理时正常,然后再用鼠标点应用程序,程序就停了……who knows the reason , pls tell me & 3ks
procedure TfrmText.Button5Click(Sender: TObject);
var
Buffer: PChar;
MemSize: Integer;
Stream: TStream;
str : WideString;
begin
with DataMConn.ADOQHistory do
begin
Close;
SQL.Clear;
SQL.Add('SELECT TempPackage FROM tPackage WHERE packageID= ''aaaaa''');
Open;
if not IsEmpty then
begin
Stream := DataMConn.ADOQHistory.CreateBlobStream(FieldByName('TempPackage'),bmRead);
try
MemSize := Stream.Size;
Inc(MemSize); //Make room for the buffer's null terminator.
Buffer := AllocMem(MemSize); //Allocate the memory.
try
Stream.Read(Buffer^, MemSize); //Read TempPackage field into buffer.
str := Buffer;
finally
FreeMem(Buffer, MemSize);
end;
finally
Stream.Free;
end;
end;
end;
end;