DIOCP开源项目-DEMO(怎么样操作远程数据库)
经过两天时间的开源,感谢网友提出一些修改意见,特别是在XE中,做了一部分优化。
可以在SVN中下载到最新的代码
https://code.google.com/p/diocp/
今天做了一个操作数据库的演示<这里我拿出我在实际项目中操作数据库的方法,集成在Demo中>
操作数据库比较简单,分两部分功能,第一部分打开SQL
procedure TfrmMain.btnOpenSQLClick(Sender: TObject); var lvJSonStream, lvRecvObject:TJsonStream; lvStream:TStream; lvData:AnsiString; l, j, x:Integer; begin lvJSonStream := TJsonStream.Create; try lvJSonStream.JSon := SO(); lvJSonStream.JSon.I['cmdIndex'] := 1001; //打开一个SQL脚本,获取数据 lvJSonStream.Json.S['sql'] := mmoSQL.Lines.Text; FClientSocket.sendObject(lvJSonStream); finally lvJSonStream.Free; end; //读取数据 lvRecvObject := TJsonStream.Create; try FClientSocket.recvObject(lvRecvObject); if not lvRecvObject.getResult then begin raise Exception.Create(lvRecvObject.getResultMsg); end; SetLength(lvData, lvRecvObject.Stream.Size); lvRecvObject.Stream.Position := 0; lvRecvObject.Stream.ReadBuffer(lvData[1], lvRecvObject.Stream.Size); cdsMain.XMLData := lvData; finally lvRecvObject.Free; end; end;
服务端ClientContext中的处理
procedure TClientContext.dataReceived(const pvDataObject:TObject); var lvJsonStream:TJSonStream; lvFile:String; lvCmdIndex:Cardinal; lvXMLData, lvEncodeData:AnsiString; lvSQL:String; begin lvJsonStream := TJSonStream(pvDataObject); lvCmdIndex := lvJsonStream.JSon.I['cmdIndex']; //echo测试 if lvCmdIndex= 1000 then begin InterlockedIncrement(TesterINfo.__RecvTimes); //回写数据 writeObject(lvJsonStream); end else if lvCmdIndex = 1001 then begin //根据sql获取一个数据,放在Stream中 try lvSQL := lvJsonStream.Json.S['sql']; lvXMLData := dmMain.CDSProvider.QueryXMLData(lvSQL); lvJsonStream.Clear(); lvJsonStream.Stream.WriteBuffer(lvXMLData[1], Length(lvXMLData)); lvJsonStream.setResult(True); except on e:Exception do begin lvJsonStream.Clear(); lvJsonStream.setResult(False); lvJsonStream.setResultMsg(e.Message); end; end; //回写数据 writeObject(lvJsonStream);
保存数据用到一个非常好用的DLL
客户端:
procedure TfrmMain.btnPostClick(Sender: TObject); var lvJSonStream, lvRecvObject:TJsonStream; lvStream:TStream; lvData:AnsiString; l, j, x:Integer; begin if cdsMain.State in [dsInsert, dsEdit] then cdsMain.Post; if cdsMain.ChangeCount = 0 then begin ShowMessage('没有做任何修改!'); exit; end; lvJSonStream := TJsonStream.Create; try lvJSonStream.JSon := SO(); lvJSonStream.JSon.I['cmdIndex'] := 1002; //打开一个SQL脚本,获取数据 //打包修改记录 with TCDSOperatorWrapper.createCDSEncode do begin setTableINfo(PAnsiChar(AnsiString(edtUpdateTable.Text)), PAnsiChar(AnsiString(edtKeyFields.Text))); setData(cdsMain.Data, cdsMain.Delta); //执行编码 Execute; lvData := getPackageData; end; mmoData.Clear; mmoData.Lines.Add(lvData); lvJSonStream.Stream.Write(lvData[1], Length(lvData)); FClientSocket.sendObject(lvJSonStream); finally lvJSonStream.Free; end; //读取数据 lvRecvObject := TJsonStream.Create; try FClientSocket.recvObject(lvRecvObject); if not lvRecvObject.getResult then begin raise Exception.Create(lvRecvObject.getResultMsg); end else begin ShowMessage('保存成功!'); end; finally lvRecvObject.Free; end; cdsMain.MergeChangeLog(); end;
服务端:
if lvCmdIndex = 1002 then //保存数据到熟客 begin try lvJsonStream.Stream.Position := 0; SetLength(lvEncodeData, lvJSonStream.Stream.Size); lvJsonStream.Stream.ReadBuffer(lvEnCodeData[1], lvJSonStream.Stream.Size); dmMain.ExecuteApplyUpdate(lvEncodeData); lvJsonStream.Clear(); lvJsonStream.setResult(True); except on e:Exception do begin lvJsonStream.Clear(); lvJsonStream.setResult(False); lvJsonStream.setResultMsg(e.Message); end; end; //回写数据 writeObject(lvJsonStream); end