标签:extract pat object pos graphics symbol tool flags inter
采用Delphi7+SQL2008
一、创建数据库和表
- CREATE TABLE [dbo].[tb_Department](
- [FKey] [uniqueidentifier] NOT NULL,
- [FName] [varchar](50) NULL,
- [FAge] [varchar](50) NULL,
- [FSex] [varchar](50) NULL,
- [FMobile] [varchar](50) NULL,
- [FRemark] [varchar](200) NULL
- ) ON [PRIMARY]
二、写服务端
2.1 先创建一个application
在窗体中添加Label如图显示
- unit ufrmMain;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
-
- type
- TfrmMain = class(TForm)
- lbl1: TLabel;
- private
-
- public
-
- end;
-
- var
- frmMain: TfrmMain;
-
- implementation
-
- {$R *.dfm}
-
- end.
2.2 File-New-Other
点击OK 在弹出的对话框中 填写
名字自己根据需要 填写
此时生成2个单元 一个Project1_TLB 和 Unit2 单元
打开Project1_TLB 单元 按F12键
在弹出的对话框中
Name就是我们要的方法名称(根据自己需要填写)GetData 获取数据
新增参数 如下图
再按相同的方法 添加PostData方法(保存数据)
最终结果如下图
添加后的最代码终结果
Unit2单元成功 添加以下
前面新增了2个接口方法 然后我们在这个单元里面 实现 方便客户端调用
代码如下
- unit Unit2;
-
- {$WARN SYMBOL_PLATFORM OFF}
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
- DBClient, Project1_TLB, StdVcl, ADODB, Provider, DB;
-
- type
- TTestService = class(TRemoteDataModule, ITestService)
- conData: TADOConnection;
- dsTemp: TClientDataSet;
- dspTemp: TDataSetProvider;
- qryTemp: TADOQuery;
- procedure RemoteDataModuleCreate(Sender: TObject);
- private
- I: Integer;
- Params: OleVariant;
- OwnerData: OleVariant;
-
- function InnerGetData(strSQL: String): OleVariant;
- function InnerPostData(Delta: OleVariant): Integer;
- protected
- class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
- procedure GetData(const Table, Where: WideString; var Ret: OleVariant);
- safecall;
- procedure PostData(const Table: WideString; Value: OleVariant;
- var Ret: OleVariant); safecall;
-
- public
-
- end;
-
- implementation
-
- {$R *.DFM}
-
- procedure TTestService.GetData(const Table, Where: WideString;
- var Ret: OleVariant);
- const SQL = ‘select * from %s where %s‘;
- begin
- Ret := Self.InnerGetData(Format(SQL, [Table, Where]));
- end;
-
-
- function TTestService.InnerGetData(strSQL: String): OleVariant;
- begin
-
- if qryTemp.Active then qryTemp.Active := False;
- Result := Self.AS_GetRecords(‘dspTemp‘, -1, I, ResetOption+MetaDataOption,
- strSQL, Params, OwnerData);
- end;
-
- function TTestService.InnerPostData(Delta: OleVariant): Integer;
- begin
- Self.AS_ApplyUpdates(‘dspTemp‘, Delta, 0, Result, OwnerData);
- end;
-
- procedure TTestService.PostData(const Table: WideString; Value: OleVariant;
- var Ret: OleVariant);
- var
- KeyField: TField;
- begin
- dsTemp.Data := Value;
- if dsTemp.IsEmpty then Exit;
-
- KeyField := dsTemp.FindField(‘FKey‘);
- if KeyField=nil then raise Exception.Create(‘ 键值字段未发现.‘);
- if KeyField.IsNull then
- begin
- qryTemp.SQL.Text := ‘select * from ‘+Table+‘ where 1>2‘;
- end
- else
- begin
- qryTemp.SQL.Text := ‘select * from ‘+Table+‘ where FKey=‘+QuotedStr(KeyField.AsString);
- qryTemp.Open;
- with qryTemp.FieldByName(‘FKey‘) do ProviderFlags := ProviderFlags + [pfInKey];
- dspTemp.UpdateMode := upWhereKeyOnly;
- end;
- qryTemp.Open;
- Ret := InnerPostData(Value);
- end;
-
- class procedure TTestService.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
- begin
- if Register then
- begin
- inherited UpdateRegistry(Register, ClassID, ProgID);
- EnableSocketTransport(ClassID);
- EnableWebTransport(ClassID);
- end else
- begin
- DisableSocketTransport(ClassID);
- DisableWebTransport(ClassID);
- inherited UpdateRegistry(Register, ClassID, ProgID);
- end;
- end;
-
-
-
- procedure TTestService.RemoteDataModuleCreate(Sender: TObject);
- begin
- Self.qryTemp.Connection := Self.conData;
- Self.dspTemp.DataSet := Self.qryTemp;
- Self.dspTemp.Options := Self.dspTemp.Options + [poAllowCommandText];
- conData.ConnectionString:=‘File Name=‘+ExtractFilePath(ParamStr(0))+‘conData.udl‘;
- try
- Self.conData.Open;
- except
- on e:Exception do
- begin
-
- end;
- end;
- end;
-
- initialization
- TComponentFactory.Create(ComServer, TTestService,
- Class_TestService, ciMultiInstance, tmApartment);
- end.
再讲讲conData.udl 文件的创建
新建一个txt文件
添加 内容
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLOLEDB.1;Password=test;Persist Security Info=True;User ID=sa;Initial Catalog=db_test;Data Source=192.168.0.1
保存 修改扩展名 为.udl 就可以了。
到此 服务端写完了
开始写客户端程序之前( 先启动scktsrvr.exe 此 在dephi程序的bin目录下 ) 然后 启动服务端
如果不想在客户的机器上注册midas.dll 请在使用ClientDataSet单元中 引用 MidasLib 单元
项目源码下载 —— http://download.csdn.net/detail/gykthh/8077801
Delphi 三层框架开发 服务端开发
标签:extract pat object pos graphics symbol tool flags inter
原文地址:http://www.cnblogs.com/hssbsw/p/7827952.html