我有一个使用 Delphi 创建的 Web 服务,我想用它连接到 sql server,所以我在项目中添加了一个 ADO Connection 和 ADOQuery,它们都已配置并准备好使用,只有一个小问题,我的项目中有两个单元,这些对象被添加到 Unit1 中,我正在使用我的 ImplUnit,而我的 ImplUnit 是另一个单元,并且找不到引用或在另一个单元中包含一个单元的方法。
单元1
{ SOAP WebModule}
unit Unit1;
interface
uses
SysUtils, Classes, HTTPApp, InvokeRegistry, WSDLIntf, TypInfo,
WebServExp, WSDLBind, XMLSchema, WSDLPub, SOAPPasInv, SOAPHTTPPasInv,
SOAPHTTPDisp, WebBrokerSOAP, DB, ADODB;
type
TWebModule1 = class(TWebModule)
HTTPSoapDispatcher1: THTTPSoapDispatcher;
HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker;
WSDLHTMLPublish1: TWSDLHTMLPublish;
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
ADOQuery1: TADOQuery;
procedure WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
WebModule1: TWebModule1;
implementation
{$R *.dfm}
procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
WSDLHTMLPublish1.ServiceInfo(Sender, Request, Response, Handled);
end;
end.
我的单位
unit UTImplementacao;
interface
uses
InvokeRegistry,DB, ADODB;
type
IInterface = interface(IInvokable)
['{EFF30FFA-DA0C-433A-832A-0BA057B55103}']
function ReceiveUser(username : String; password : String) :
Boolean; stdcall;
end;
TImplementacao = class(TInvokableClass, IInterface)
public
function ReceiveUser(username : String; password : String) :
Boolean; stdcall;
end;
implementation
{ TImplementacao }
function TImplementacao.ReceiveUser(username, password: String): Boolean;
var
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
begin
try
ADOConnection1 := TADOConnection.Create(nil);
ADOConnection1.LoginPrompt := False;
ADOConnection1.ConnectionString:= 'Provider=SQLOLEDB.1;Integrated Security=SSPI;' +
'Persist Security Info=False;' +
'User ID=Diego;'+
'Catalog=OnlineShopping;' +
'Data Source=DIEGO-PC\SQLEXPRESS'+
';Use Procedure for Prepare=1;' +
'Auto Translate=True;Packet Size=4096;'+
'Workstation ID=DIEGO-PC;'+
'Use Encryption for Data=False;'+
'Tag with column collation when possible=False;';
ADOConnection1.Connected := True;
ADOQuery1.Connection := ADOConnection1;
ADOQuery1.SQL.Add('select username,upassword from Users '+
'where username = :usernamep and upassword = '+
':upasswordp');
ADOQuery1.Parameters.ParamByName('upasswordp').Value := password;
ADOQuery1.Parameters.ParamByName('usernamep').Value := username;
ADOQuery1.ExecSQL;
Result := True;
finally
ADOQuery1.Free;
if ADOConnection1.Connected then
ADOConnection1.Close;
ADOConnection1.Free;
end;
Result := False;
end;
initialization
InvRegistry.RegisterInvokableClass(TImplementacao);
InvRegistry.RegisterInterface(TypeInfo(IInterface));
end.
请忽略我添加到我的单元的 ADOConnection 和 ADOQuery 我有一点绝望的广告重复代码......是的,我知道游艇!!!!
@银战士
如果在 UTImplementacao 的使用中声明 Unit1,我将可以访问以下组件:
type
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
ADOQuery1: TADOQuery;
或者我应该为 var 子句中的每个类型变量声明?