using
System;
using
System.Web;
using
System.Web.Services;
using
System.Web.Services.Protocols;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using
System.Data;
using
System.Data.OracleClient;
using
System.IO;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using
System.IO.Compression;
using
System.Runtime.Serialization.Formatters.Binary;
[WebServiceBinding(ConformsTo
=
WsiProfiles.BasicProfile1_1)]
public
class
Service : System.Web.Services.WebService
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private OracleConnection webConnection;
DataTable curDt = new DataTable();
DataSet curSet = new DataSet();
OracleDataAdapter curDa = new OracleDataAdapter();
OracleCommand curComm = new OracleCommand();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string GetCurCnnStr()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//连接数据库并打开
OracleConnectionStringBuilder cnnStrBuilder = new OracleConnectionStringBuilder();
cnnStrBuilder.UserID = "";
cnnStrBuilder.Password = "";
cnnStrBuilder.DataSource = "";
return cnnStrBuilder.ConnectionString;
}
public Service()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[WebMethod(Description = "判断空值")]
private OracleParameter CreateParam(string ParamName, object ParamValue)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
OracleParameter Result = new OracleParameter();
Result.ParameterName = ParamName;
if (ParamValue != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Result.Value = ParamValue;
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Result.Value = DBNull.Value;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Result;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[WebMethod(Description = "测试连接")]
public string Linking()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "WebService连接成功!";
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[WebMethod(Description = "查询信息")]
public byte[] Search(string img)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
webConnection = new OracleConnection(GetCurCnnStr());
webConnection.Open();
//查询表中的所有的数据
OracleDataAdapter webAdapter = new OracleDataAdapter("select pic from pic where id=" + img, webConnection);
DataSet webDataTable = new DataSet();
//byte[] webDataTable = new byte[0];
webAdapter.Fill(webDataTable);
webConnection.Close();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//序列化为二进制
webDataTable.RemotingFormat = SerializationFormat.Binary;//确定序列化格式
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
bFormatter.Serialize(mStream, webDataTable);
byte[] bytes = mStream.ToArray();//将数据流写入字节数组
//返回数组
return bytes;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
curComm.Transaction.Rollback();
return null;
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
webConnection.Close();
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[WebMethod(Description = "添加信息")]
public void Add(string id, byte[] Image)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
webConnection = new OracleConnection(GetCurCnnStr());
curComm.Connection = webConnection;
webConnection.Open();
curComm.Transaction = webConnection.BeginTransaction();
curComm.CommandText = "insert into pic(id,pic)values(:pId,:pPic)";
curComm.Parameters.Add(CreateParam("pId", (id != null) ? id : null));
curComm.Parameters.Add(CreateParam("pPic", (Image != null) ? Image : null));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
curComm.ExecuteNonQuery();
curComm.Transaction.Commit();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
curComm.Transaction.Rollback();
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
webConnection.Close();
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
以上为webservice客户端代码;下面是delphi代码:
unit Unit1;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
interface
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, InvokeRegistry, Rio, SOAPHTTPClient, ExtCtrls, StdCtrls,
Service,types,jpeg,StrUtils, Buttons;
//
StrUtils:取指定字符
{QDialogs:在delphi6及以后的版本中,
为了支持kylix,能使程序在linux下运行
每一个单元都增加了对应的QDialogs单元
如果你用了带Q的单元,哪么如果你的程序
要想在Windows中正常运行必须得有qtintf.dll
的支持你可以搜索一个,把程序中uses中的
单元前有Q的把Q去掉就ok了! }
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
type
TForm1
=
class(TForm)
Label1: TLabel;
Edit1: TEdit;
Panel1: TPanel;
Image1: TImage;
HTTPRIO1: THTTPRIO;
Open
: TOpenDialog;
Search: TButton;
Add
: TButton;
Update
: TButton;
Delete
: TButton;
Panel2: TPanel;
Image2: TImage;
Label2: TLabel;
procedure
SearchClick(Sender: TObject);
procedure
FormShow(Sender: TObject);
procedure
Image2Click(Sender: TObject);
procedure
Edit1KeyPress(Sender: TObject;
var
Key
:
Char
);
procedure
Image1Click(Sender: TObject);
procedure
AddClick(Sender: TObject);
procedure
DeleteClick(Sender: TObject);
procedure
UpdateClick(Sender: TObject);
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private
{ Private declarations }
public
{
Public
declarations }
end
;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
var
Form1: TForm1;
files:TFileStream;
ms:TMemoryStream;
jpg:TjpegImage;
s:TByteDynArray;
path:WideString;
implementation
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
{$R
*
.dfm}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
procedure
TForm1.SearchClick(Sender: TObject);
var
bmTemp:TjpegImage;
//
TBitmap;
Web_Search:ServiceSoap;
begin
bmTemp:
=
TjpegImage.
Create
;
//
创建流
ms:
=
TMemoryStream.
Create
;
Web_Search:
=
HTTPRIO1
as
ServiceSoap;
try
if
(edit1.
Text
<>
''
)
then
begin
//
调用服务查询功能
s:
=
(Web_Search.Search(edit1.
Text
));
if
(length(s)
<
6229
)
then
begin
showmessage(
'
没有图片,请重输
'
);
image1.Picture.Assign(nil);
end
else
begin
//
从图片开始位置读取
ms.Write(s
[
6229
]
,length(s));
ms.Position:
=
0
;
//
加载流
bmTemp.LoadFromStream(ms);
//
输出
image1.Picture.Assign(bmTemp);
end
;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
end
else
begin
showmessage(
'
缺少查询条件!
'
);
image1.Picture.Assign(nil);
edit1.SetFocus;
end
;
finally
//
释放空间
ms.Free;
bmTemp.Free;
//
控制
//
edit1.Clear;
edit1.SetFocus;
end
;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
end
;
procedure
TForm1.FormShow(Sender: TObject);
var
fstyle: dWord;
begin
//
控制edit只输入数字
fstyle :
=
GetWindowLong(Edit1.Handle, GWL_STYLE);
SetWindowLong(Edit1.Handle, GWL_STYLE, fstyle
or
ES_NUMBER);
//
指定图片格式
edit1.SetFocus;
end
;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
procedure
TForm1.Image2Click(Sender: TObject);
var
Web_link:ServiceSoap;
begin
//
测试连接
Web_link:
=
HTTPRIO1
as
ServiceSoap;
showmessage(
''
+
Web_link.Linking
+
''
);
end
;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
procedure
TForm1.Edit1KeyPress(Sender: TObject;
var
Key
:
Char
);
begin
//
输入控制
if
key
=
#
13
then
Search.Click;
end
;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
procedure
TForm1.Image1Click(Sender: TObject);
var
testpath:string;
testname:string;
begin
ms:
=
TMemoryStream.
Create
;
jpg:
=
TjpegImage.
Create
;
if
Open
.
Execute
then
begin
testpath:
=
ExtractFileName(
open
.FileName);
testname:
=
RightStr(testpath,
4
);
if
(testname
=
'
.jpg
'
)
or
( testname
=
'
.JPG
'
)
or
( testname
=
'
.jpeg
'
)
or
( testname
=
'
.JPEG
'
)
then
begin
path:
=
ExtractFileDir(
open
.FileName)
+
'
\
'
+
ExtractFileName(
open
.FileName);
files:
=
TFileStream.
Create
(
open
.FileName,fmShareDenyWrite);
try
jpg.LoadFromStream(files);
jpg.SaveToStream(ms);
image1.Picture.Assign(jpg);
finally
files.Free;
end
;
end
else
showmessage(
'
当前默认为JPEG类型图片
'
);
end
;
end
;
end
.