mORMot访问远程数据库

mORMot访问远程数据库

mORMot中提供了TOleDBJetConnectionProperties类来处理Access的mdb数据库的访问,自带线程池。
通过TSQLDBServerHttpApi类,我们可以把这个TOleDBJetConnectionProperties
发布给远程的外网来访问使用。服务器端要引用SynDB,SynDBRemote,SynOleDB,SynDBMidasVCL 这几个单元。
TSQLDBServerHttpApi 使用了微软IIS的http.sys通信库,稳定高效。

代码如下:
[delphi] view plain copy
unit FrmServerForm;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,SynDB,SynDBRemote,SynOleDB,SynDBMidasVCL;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
DataBase:TSQLDBConnectionProperties;
DBServer: TSQLDBServerAbstract;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.FormCreate(Sender: TObject);
begin
DataBase := TOleDBJetConnectionProperties.Create('house.mdb;Jet OLEDB:Database Password=8888;','','','');
DBServer := TSQLDBServerHttpApi.Create(DataBase,'syndbremote','8092','admin','admin888',False);//将database连接到数据库house.mdb发布给远程客户端
//访问,远程客户端访问时的库名syndbremote,端口 8092,用户名 amin,密码 admin888
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(DataBase);
FreeAndNil(DBServer);
end;

end.

如上,简单的几句代码编译运行后,远程客户端就可以通过TSQLDBWinHTTPConnectionProperties
类来远程访问处理服务器上的数据。TSQLDBWinHTTPConnectionProperties创建类实例时,需要指定服务器ip,访问的数据库名,端口,用户名,密码
constructor TSQLDBWinHTTPConnectionProperties.Create(const aServerName, aDatabaseName, aUserID, aPassWord: RawUTF8);
通过引用 SynDBMidasVCL单元的TSynDBDataSet类,可以实现类似于TAdoquery类的调用方式,客户端代码如下:
[delphi] view plain copy
unit FrmClientUnt;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids,
Data.DB, Vcl.ExtCtrls,SynDB,SynDBRemote,SynDBDataset,SynDBMidasVCL;

type
TForm2 = class(TForm)
Panel1: TPanel;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
Props:TSQLDBWinHTTPConnectionProperties;
cds:TSynDBDataSet;
begin
Props := TSQLDBWinHTTPConnectionProperties.Create('127.0.0.1:8092','syndbremote','admin','admin888');
cds := TSynDBDataSet.Create(Self);
cds.Connection := Props;//设置TSynDBDataSet的Connection
cds.CommandText :='select * from HouseInfo '; //定义一个查询串
cds.Open;
DataSource1.DataSet := cds; //给DbGrid的DataSoured数据库的Dataset设置为TSynDBDataSet,将查询结果显示在DBGrid中

end;

end.

 
posted @ 2017-12-15 09:52  delphi中间件  阅读(1655)  评论(0编辑  收藏  举报