马上周末了,趁着下午这会儿回顾一下这几天对旧项目的升级过程,一些重要但不常用的东西记录下来是很有必要的。其中一个项目中对KBMMW的远程数据通讯方式做了改进,利用SampleService/SampleClient方式传输数据集,以增加对底层数据通讯的可控性。

服务端代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 PerformOpenSQL(ClientIdent:TkbmMWClientIdentity; const Args:array of Variant):Variant; virtual;
 
  public
     { Public declarations }
{$IFNDEF CPP}class{$ENDIF} function GetFlags:TkbmMWServiceFlags; override;
  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='OPENSQL' then
        Result:=PerformOpenSQL(ClientIdent,Args)
     else Result:=inherited ProcessRequest(Func,ClientIdent,Args);
end;
 
function TkbmMWSimpleService1.PerformOpenSQL(ClientIdent:TkbmMWClientIdentity; const Args:array of Variant):Variant;
var
  sqlStr: string;
  aQuery: TUniQuery;
  aconn: TkbmMWUNIDACConnection;
  memTable: TkbmMemTable;
  aStreamFormat: TkbmBinaryStreamFormat;
begin
  Result := 0;
  sqlStr:=args[0];
  aQuery := TUniQuery.Create(nil);//uniquery,和两层的用法一样
  aQuery.Options.QueryRecCount := True;
  aconn := TkbmMWUNIDACConnection(DMunt.kbmMWUNIDACConnectionPool1.GetBestConnection(True,0,nil,10000));//取连接池中的连接
  if aconn = nil then
  begin
    kbmMWRaiseServerException('无可用的数据库连接');
    Exit;
  end;
  aQuery.Connection := aconn.Database;
  aQuery.SQL.Text := sqlStr;
  if (mainform.PAL_mode_01.Visible) then
    LogIOer.AddShow(ClientIdent.Username+'执行SQL查询:'+aQuery.SQL.Text,0);
  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;//返回记录条数
    except
      on E:Exception do
      begin
        kbmMWRaiseServerException(E.Message+',异常语句:'+aQuery.SQL.Text);//抛异常到客户端
        Exit;
      end;
    end;
    memTable.LoadFromDataSet(aQuery,[mtcpoStructure,mtcpoProperties]); //复制数据集进KbMemTable
    memTable.SaveToStreamViaFormat(ResultStream,aStreamFormat); //按照指定流格式存入结果流ResultStream
  finally
    aconn.UnlockConnection;
    aQuery.Free;
    memTable.Free;
    aStreamFormat.Free;
  end;
end;

客户端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function openSql(Sqlstr:string;var Ds:TDataSet;var Rs:string):integer;stdcall;
  var
    args: array[0..1] of Variant;
  begin
    Result := 0;
    Rs := '执行成功';
    if assigned(kbmMWSimpleClient1) then
    begin
      try
        args[0]:= Sqlstr;//SQL语句
        Result := kbmMWSimpleClient1.Request('TkbmMWSimpleService1','','openSql',args);//SimpleClient执行请求
        kbmMemTable.EmptyTable;//清空内存表
        kbmMemTable.LoadFromStreamViaFormat(kbmMWSimpleClient1.ResultStream,aStreamFormat);//将结果流复制进内存表
        ds := kbmMemTable;//返回dataset(kbmMemTable继承自Tdataset)
      except
        on E:Exception do
        begin
          Result := -1;
          rs := errorInfo(E.Message);
          if FIsLog then Writelog(rs);
        end;
      end;
    end
    else
    begin
      Result := -1;//未执行初始化操作
      Rs := '远程数据通讯接口未执行初始化操作'
    end;
  end;

核心代码就这些,相信用到的人能够看明白。同理,可以利用这种方式实现二进制文件流(如:图像等)的传输,不再赘述。
另外,有一个小问题折磨了我一下午,提醒大家一下,希望大家不要像我一样粗心:
有两个类:TkbmBinaryStreamFormat(kbmMemBinaryStreamFormat.pas)、TkbmMWBinaryStreamFormat(kbmMWBinaryStreamFormat.pas)很容易混淆(正确用法见上述代码),而且一旦混淆,会造成KbmMeMTable在流的处理过程中出错。

http://www.pfeng.org/archives/385

KBMMW SampleService/SampleClient方式传输数据集的更多相关文章

  1. Node.js初探之GET方式传输

    Node.js初探之GET方式传输 例子:form用GET方法向后台传东西 html文件: <form action="http://localhost:8080/aaa" ...

  2. 流,用声明性的方式处理数据集 - 读《Java 8实战》

    引入流 Stream API的代码 声明性 更简洁,更易读 可复合 更灵活 可并行 性能更好 流是什么? 它允许以声明方式处理数据集合 遍历数据集的高级迭代器 透明地并行处理 简短定义:从支持数据处理 ...

  3. C# Post方式传输报文,和处理响应

    public string DoPost(string url, string data) { HttpWebRequest req = GetWebRequest(url, "POST&q ...

  4. Node.js初探之POST方式传输

    小知识:POST比GET传输的数据量大很多 POST发数据--"分段" 实例: 准备一个form.html文件: <!DOCTYPE html> <html> ...

  5. ASP.NET WebServce项目下添加Http服务,支持Get,Post请求方式;传输格式json/xml

    由于WEBServce老项目中需要增添新的接口,而且添加的接口不希望被其它项目以引用Servces方式使用. 那么得在现有Service项目中添加Http请求方式来实现系统间数据交互.只需要告知请求地 ...

  6. http使用formData方式传输文件请求

    转载请注明出处: 项目中有遇到http使用formData请求传输文件,在此记录一下 1.依赖jar包: <dependency> <groupId>org.apache.ht ...

  7. 用socket方式传输Image和Sound文件

    import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.Obje ...

  8. cache支持single/increment/increment4三种方式传输

    1.cache bypass signle---data length 已知 increment ---data length 不知 用 last data address  结束数据传输 2.cac ...

  9. json和jsonp的传输方式

    jsonp传输会解决跨域的问题 $.ajax({ async: false, /* url: "http://127.0.0.1:8080/2015020601/background/mea ...

随机推荐

  1. localstroge可以在页面间传递数值;

    连接地址为:http://4.suancai.sinaapp.com/localstorg/a.html 原理是,a页面设置了sessionstorge,b页面可以访问到; 并且已关闭浏览器,sest ...

  2. 超级密码(bfs)

    超级密码 Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  3. hdu 4612 Warm up 双连通缩点+树的直径

    首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...

  4. 倒计时IE6+

    很简单的 下面是我为了做多个倒计时更改之后的 dome 下载链接   兼容 IE7以上 IE6没测试应该没问题 http://yunpan.cn/cf29rxmGKuMyJ  提取码 ca61

  5. Jquery ajax调用后台aspx后台文件方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. (1)通过aspx.cs的静态方法+WebMethod进行处理 简单的介绍下 ...

  6. ##DAY7 UINavigationController

    ##DAY7 UINavigationController #pragma mark ———————UINavigationController——————————— 概括: 导航视图控制器也是一个视 ...

  7. python re(正则模块)

    参考文档:http://blog.csdn.net/wusuopubupt/article/details/29379367 ipython环境中,输入"?re",官方解释如下: ...

  8. 09-C语言数组

    目录: 一.使用xcode编辑工具 二.数组 三.数组遍历 四.多维数组 回到顶部 一.使用xcode编辑工具 1 打开xcode程序 2 创建一个项目 OSX -> Application - ...

  9. 关于地址的理解 C++

    #include <iostream> using namespace std; int main(){ ; int* ptr; ptr=&a; cout<<& ...

  10. 阿里云ECS每天一件事D1:配置SSH

    近期因为项目需求,采购了两台阿里云ECS,选择的系统为CentOS 6.3 X64 安全加固版,额外买了160G的硬盘,应该够应付此项目的需求了. ECS默认已经配置好了sshd服务,可以使用root ...