熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

Delphi調用WebServices(C#)代碼

2022-06-13   來源: ASP編程 

  using System;
using SystemWeb;
using SystemWebServices;
using SystemWebServicesProtocols;

using SystemData;
using SystemDataOracleClient;
using SystemIO;

using SystemIOCompression;
using SystemRuntimeSerializationFormattersBinary;
[WebServiceBinding(ConformsTo = WsiProfilesBasicProfile_)]
public class Service : SystemWebServicesWebService
{
private OracleConnection webConnection;
DataTable curDt = new DataTable();
DataSet curSet = new DataSet();
OracleDataAdapter curDa = new OracleDataAdapter();
OracleCommand curComm = new OracleCommand();

private string GetCurCnnStr()
{
//連接數據庫並打開
OracleConnectionStringBuilder cnnStrBuilder = new OracleConnectionStringBuilder();
cnnStrBuilderUserID = ;
cnnStrBuilderPassword = ;
cnnStrBuilderDataSource = ;
return cnnStrBuilderConnectionString;
}
public Service()
{

//如果使用設計的組件請取消注釋以下行
//InitializeComponent();
}

[WebMethod(Description = 判斷空值)]
private OracleParameter CreateParam(string ParamName object ParamValue)
{
OracleParameter Result = new OracleParameter();
ResultParameterName = ParamName;
if (ParamValue != null)
{
ResultValue = ParamValue;
}
else
{
ResultValue = DBNullValue;
}

return Result;
}

[WebMethod(Description = 測試連接)]
public string Linking()
{
return WebService連接成功!;
}


[WebMethod(Description = 查詢信息)]
public byte[] Search(string img)
{
try
{
webConnection = new OracleConnection(GetCurCnnStr());
webConnectionOpen();
//查詢表中的所有的數據
OracleDataAdapter webAdapter = new OracleDataAdapter(select pic from pic where id= + img webConnection);
DataSet webDataTable = new DataSet();
//byte[] webDataTable = new byte[];
webAdapterFill(webDataTable);
webConnectionClose();

//序列化為二進制
webDataTableRemotingFormat = SerializationFormatBinary;//確定序列化格式
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
bFormatterSerialize(mStream webDataTable);
byte[] bytes = mStreamToArray();//將數據流寫入字節數組
//返回數組
return bytes;

}
catch (Exception)
{
curCommTransactionRollback();
return null;
}
finally
{

webConnectionClose();
}

}

[WebMethod(Description = 添加信息)]
public void Add(string id byte[] Image)
{
try
{
webConnection = new OracleConnection(GetCurCnnStr());
curCommConnection = webConnection;
webConnectionOpen();
curCommTransaction = webConnectionBeginTransaction();
curCommCommandText = insert into pic(idpic)values(:pId:pPic);
curCommParametersAdd(CreateParam(pId (id != null) ? id : null));
curCommParametersAdd(CreateParam(pPic (Image != null) ? Image : null));

curCommExecuteNonQuery();
curCommTransactionCommit();


}
catch (Exception)
{
curCommTransactionRollback();
}
finally
{
webConnectionClose();
}
}

}

  unit Unit;

interface

uses
Windows Messages SysUtils Variants Classes Graphics Controls Forms
Dialogs InvokeRegistry Rio SOAPHTTPClient ExtCtrls StdCtrls
ServicetypesjpegStrUtils Buttons;
//StrUtils:取指定字符
{QDialogs:在delphi及以後的版本中
為了支持kylix能使程序在linux下運行
每一個單元都增加了對應的QDialogs單元
如果你用了帶Q的單元哪麼如果你的程序
要想在Windows中正常運行必須得有qtintfdll
的支持你可以搜索一個把程序中uses中的
單元前有Q的把Q去掉就ok了! }

type
TForm = class(TForm)
Label: TLabel;
Edit: TEdit;
Panel: TPanel;
Image: TImage;
HTTPRIO: THTTPRIO;
Open: TOpenDialog;
Search: TButton;
Add: TButton;
Update: TButton;
Delete: TButton;
Panel: TPanel;
Image: TImage;
Label: TLabel;
procedure SearchClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ImageClick(Sender: TObject);
procedure EditKeyPress(Sender: TObject; var Key: Char);
procedure ImageClick(Sender: TObject);
procedure AddClick(Sender: TObject);
procedure DeleteClick(Sender: TObject);
procedure UpdateClick(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form: TForm;
files:TFileStream;
ms:TMemoryStream;
jpg:TjpegImage;
s:TByteDynArray;
path:WideString;
implementation

{$R *dfm}

procedure TFormSearchClick(Sender: TObject);
var
bmTemp:TjpegImage;//TBitmap;
Web_Search:ServiceSoap;
begin
bmTemp:=TjpegImageCreate;
//創建流
ms:=TMemoryStreamCreate;
Web_Search:=HTTPRIO as ServiceSoap;
try
if (editText<>) then
begin
//調用服務查詢功能
s:=(Web_SearchSearch(editText));
if(length(s)<) then
begin
showmessage(沒有圖片請重輸);
imagePictureAssign(nil);
end
else
begin
//從圖片開始位置讀取
msWrite(s[]length(s));
msPosition:=;
//加載流
bmTempLoadFromStream(ms);
//輸出
imagePictureAssign(bmTemp);
end;

end
else
begin
showmessage(缺少查詢條件!);
imagePictureAssign(nil);
editSetFocus;
end;
finally
//釋放空間
msFree;
bmTempFree;
//控制
//editClear;
editSetFocus;
end;

end;
procedure TFormFormShow(Sender: TObject);
var
fstyle: dWord;
begin
//控制edit只輸入數字
fstyle := GetWindowLong(EditHandle GWL_STYLE);
SetWindowLong(EditHandle GWL_STYLE fstyle or ES_NUMBER);
//指定圖片格式
editSetFocus;

end;

procedure TFormImageClick(Sender: TObject);
var
Web_link:ServiceSoap;
begin
//測試連接
Web_link:=HTTPRIO as ServiceSoap;
showmessage(+Web_linkLinking+);
end;

procedure TFormEditKeyPress(Sender: TObject; var Key: Char);
begin
//輸入控制
if key=# then
SearchClick;
end;

procedure TFormImageClick(Sender: TObject);
var
testpath:string;
testname:string;
begin
ms:=TMemoryStreamCreate;
jpg:=TjpegImageCreate;
if OpenExecute then
begin
testpath:=ExtractFileName(openFileName);
testname:=RightStr(testpath);
if (testname=jpg)or( testname=JPG) or( testname=jpeg)or( testname=JPEG)then
begin
path:=ExtractFileDir(openFileName)+\+ExtractFileName(openFileName);
files:= TFileStreamCreate(openFileNamefmShareDenyWrite);
try
jpgLoadFromStream(files);
jpgSaveToStream(ms);
imagePictureAssign(jpg);
finally
filesFree;
end;
end
else
showmessage(當前默認為JPEG類型圖片);
end;
end;
end


From:http://tw.wingwit.com/Article/program/ASP/201311/21728.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.