DIOCP开源项目-DEMO(怎么样操纵长途数据库)

    添加时间:2013-5-21 点击量:

    经过两天时候的开源,感激网友提出一些批改定见,希罕是在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);
    //履行编码
    ute;
    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.uteApplyUpdate(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



    读书,不要想着实用,更不要有功利心。读书只为了自身的修养。邂逅一本好书如同邂逅一位知己,邂逅一个完美之人。有时心生敬意,有时怦然心动。仿佛你心底埋藏多年的话,作者替你说了出来,你们在时光深处倾心相遇的一瞬间,情投意合,心旷神怡。
    分享到: