• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

用delphi 做服务器,basic4android 实现三层数据交互

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

这是我第一次写博客,写得不好,以前学的东西我从来就没有想过记录下来,使用很快就忘记了,所以我现在开始写博客把所有学到的东西都要记录下来,加油...


首先delphi 里面加入kbmMWClient 这个插件 下载地址:http://pan.baidu.com/share/link?shareid=2712849152&uk=3993363350

第一个页面

unit unt_main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, kbmMWCustomTransport, kbmMWServer, kbmMWTCPIPIndyServerTransport,
StdCtrls;

type
Tmainform = class(TForm)
kbmMWServer1: TkbmMWServer;
kbmMWTCPIPIndyServerTransport1: TkbmMWTCPIPIndyServerTransport;
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
mainform: Tmainform;

implementation

uses unt_service_func, unt_dm;

{$R *.dfm}

procedure Tmainform.Button1Click(Sender: TObject);
begin
//链接数据库
DataModule1.ADOConnection1.ConnectionString := Edit1.Text;
DataModule1.ADOConnection1.Connected := True;
//启动服务
kbmMWServer1.Active := True;
end;

procedure Tmainform.FormCreate(Sender: TObject);
var
sd: TKbmMWCustomServiceDefinition;
begin
//项目启动时先要注册服务
sd := kbmMWServer1.RegisterService(TkbmMWSimpleService1,false);
end;

end.


第 二 个页面

unit unt_service_func;

// =========================================================================
// kbmMW - An advanced and extendable middleware framework.
// by Components4Developers (http://www.components4developers.com)
//
// Service generated by kbmMW service wizard.
//
// INSTRUCTIONS FOR REGISTRATION/USAGE
// -----------------------------------
// Please update the uses clause of the datamodule/form the TkbmMWServer is placed on by adding
// YourServiceUnitName to it. Eg.
//
// uses ...,kbmMWServer,YourServiceUnitName;
//
// Somewhere in your application, make sure to register the serviceclass to the TkbmMWServer instance. Eg.
//
// var
// sd:TkbmMWCustomServiceDefinition;
// ..
// sd:=kbmMWServer1.RegisterService(yourserviceclassname,false);
//
// Set the last parameter to true if this is the default service.
//

 

{$I kbmMW.inc}

interface

uses
SysUtils,
{$ifdef LEVEL6}
Variants,
{$else}
Forms,
{$endif}
Classes,
kbmMWSecurity,
kbmMWServer,
kbmMWServiceUtils,
kbmMWGlobal,
kbmMWADOX,DB, ADODB,kbmMemTable, kbmMemBinaryStreamFormat;

type
TkbmMWSimpleService1 = class(TkbmMWSimpleService)
private
{ Private declarations }
protected
{ Protected declarations }
//新增以下函数
function ProcessRequest(const Func:string; const ClientIdent:TkbmMWClientIdentity; const Args:array of Variant):Variant; override;
function PerformUpdate(ClientIdent:TkbmMWClientIdentity; const Args:array of Variant):Variant; virtual;
function PerformSelect(ClientIdent:TkbmMWClientIdentity; const Args:array of Variant):Variant; virtual;
public
{ Public declarations }
{$IFNDEF CPP}class{$ENDIF} function GetPrefServiceName:string; override;
{$IFNDEF CPP}class{$ENDIF} function GetVersion:string; override;
{$IFNDEF CPP}class{$ENDIF} function GetFlags:TkbmMWServiceFlags; override;
end;

implementation

uses kbmMWExceptions, unt_main, unt_dm;

{$R *.dfm}


// Service definitions.
//---------------------

{$IFNDEF CPP}class{$ENDIF} function TkbmMWSimpleService1.GetPrefServiceName:string;
begin
Result:='demoservice';
end;

{$IFNDEF CPP}class{$ENDIF} function TkbmMWSimpleService1.GetVersion:string;
begin
Result:='1.0';
end;

{$IFNDEF CPP}class{$ENDIF} function TkbmMWSimpleService1.GetFlags:TkbmMWServiceFlags;
begin
Result:=[mwsfListed];
end;

function TkbmMWSimpleService1.ProcessRequest(const Func:string; const ClientIdent:TkbmMWClientIdentity; const Args:array of Variant):Variant;
var
AFunc:string;
begin
AFunc:=UpperCase(Func);
if AFunc='UPDATE' then // 关联到PerformUpdate函数
Result:=PerformUpdate(ClientIdent,Args)
else
if AFunc='SELECT' then // 关联到PerformSelect 函数
Result:=PerformSelect(ClientIdent,Args)
else Result:=inherited ProcessRequest(Func,ClientIdent,Args);
end;

function TkbmMWSimpleService1.PerformUpdate(ClientIdent:TkbmMWClientIdentity; const Args:array of Variant):Variant;
var
param: string;
aQuery: TADOQuery;
aconn: TkbmMWADOXConnection;
begin
Result := 0;
param :=args[0]; //取出参数
aQuery := TADOQuery.Create(nil);
//从数据库连接池中取出可用链接
aconn := TkbmMWADOXConnection(DataModule1.kbmMWADOXConnectionPool1.GetBestConnection(True,0,nil,10000));
if aconn = nil then
begin
kbmMWRaiseServerException('无可用的数据库连接');
Exit;
end;
aQuery.Connection := aconn.Database;
aQuery.SQL.Text := 'update devinfo set dev_name=:param where id < 10';
aQuery.Parameters.ParamByName('param').Value := param; //传入参数
try
try
aQuery.ExecSQL;
Result := aQuery.RowsAffected;//返回影响条数
except
Result := -1; //错误时返回-1
end;
finally
aconn.UnlockConnection;
aQuery.Free;
end;
end;

function TkbmMWSimpleService1.PerformSelect(ClientIdent:TkbmMWClientIdentity; const Args:array of Variant):Variant;
var
param: string;
aQuery: TADOQuery;
aconn: TkbmMWADOXConnection;
memTable: TkbmMemTable; //内存表类
aStreamFormat: TkbmBinaryStreamFormat; //流格式类
begin
Result := 0;
param :=args[0]; //取出参数
aQuery := TADOQuery.Create(nil);
//从数据库连接池中取出可用链接
aconn := TkbmMWADOXConnection(DataModule1.kbmMWADOXConnectionPool1.GetBestConnection(True,0,nil,10000));
if aconn = nil then
begin
kbmMWRaiseServerException('无可用的数据库连接');
Exit;
end;
aQuery.Connection := aconn.Database;
aQuery.SQL.Text := 'select * from devinfo where dev_name=:param';
aQuery.Parameters.ParamByName('param').Value := param; //传入参数
//初始化内存表
memTable := TkbmMemTable.Create(nil);
//初始化流格式
aStreamFormat := TkbmBinaryStreamFormat.Create(nil);
//内存表设置
memTable.DefaultFormat := aStreamFormat;
memTable.IndexFieldNames := '';
memTable.SortFields := '';
memTable.MasterSource := nil;
try
try
aQuery.Open;
Result := aQuery.RecordCount;//返回记录条数
//导入结果到流
memTable.LoadFromDataSet(aQuery,[mtcpoStructure,mtcpoProperties]);
//格式化流
memTable.SaveToStreamViaFormat(ResultStream,aStreamFormat);
except
Result := -1; //错误时返回-1
end;
finally
aconn.UnlockConnection;
aQuery.Free;
memTable.Free;
aStreamFormat.Free;
end;
end;


end.


第三个 页面

 

unit unt_dm;

interface

uses
SysUtils, Classes, kbmMWCustomConnectionPool, kbmMWCustomSQLMetaData,
kbmMWMSSQLMetaData, DB, ADODB, kbmMWADOX;

type
TDataModule1 = class(TDataModule)
kbmMWADOXConnectionPool1: TkbmMWADOXConnectionPool;
ADOConnection1: TADOConnection;
kbmMWMSSQLMetaData1: TkbmMWMSSQLMetaData;
kbmMWPooledSession1: TkbmMWPooledSession;
private
{ Private declarations }
public
{ Public declarations }
end;

var
DataModule1: TDataModule1;

implementation

{$R *.dfm}

end.

这些每加入一个页面最好、F9 一下 看看错在那 

如果有一些页面时经常报错的 加入

这样服务器做好了...明天再发android 客户端的


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab练习程序(Kruskal最小生成树)发布时间:2022-07-18
下一篇:
Matlab:结构数组发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap