!!!!C/S《进销存单据主从表的存储》不是真正的高手不要乱讲!!!!!(100)

  • 主题发起人 主题发起人 violetxh
  • 开始时间 开始时间
V

violetxh

Unregistered / Unconfirmed
GUEST, unregistred user!
使用ClientDataSet的ApplyUpdates(0)保存从表数据时,有时部分数据不能存入到数据库中我使用的是SQL数据库安装在一台服务器中。后来我用一个函数将ClientdataSet的数据变化转换成SQL语句,这样主表是SQL语句,从表也是SQL语句,分别发送到服务器,保存成功,但我感觉这样很麻烦,同时效率也不高//获取ClientDataset.delta的SQL语句function GetSqlS(AdoCon:TADOConnection; pdelta: OleVariant; const ptablename, pkeyfields: WideString): WideString; function vartosql(value: Variant): wideString; var tmp:widestring; begin if (varisnull(Value)) or (varisempty(Value)) then Result:='NULL' else case Vartype(value) of varDate: begin tmp := formatDatetime('yyyy-mm-dd hh:mm:ss', VartoDatetime(Value)); Result:=Quotedstr(tmp); end; varString,varOlestr: Result:=Quotedstr(Trim(Vartostr(Value))); varboolean: begin if Value then Result:='1' else Result:='0'; end; varSmallint,varInteger,varDouble,varShortInt,varInt64,varLongWord,varCurrency: begin Result:=trim(Vartostr(Value)); end; else Result:=Quotedstr(Trim(Vartostr(Value))); end; end; var i, j: integer; s1, s2: string; Cmdstr: string; FieldList, Keylist: TstringList; Cdsupdate: TClientDataSet; sqlstr: WideString; ado: TADOQuery;begin if varisnull(pdelta) then Exit; Cdsupdate:=TClientDataSet.Create(nil); Cdsupdate.data:=pdelta; if not Cdsupdate.Active then Cdsupdate.Open; try FieldList:=TstringList.Create; Keylist:=TstringList.Create; Keylist.Delimiter:=','; Keylist.DelimitedText:=pkeyfields; ado:=TADOQuery.Create(nil); ado.Connection:=AdoCon; ado.sql.Text:='select * from '+ptablename+' where 1=0'; ado.Open; ado.GetFieldNames(FieldList); FreeAndNil(ado); for i:=1 to FieldList.Count do if Cdsupdate.FindField(FieldList[i-1])<>nil then Cdsupdate.FindField(FieldList[i-1]).tag:=1; FreeAndNil(FieldList); if Cdsupdate.RecordCount>0 then begin Cdsupdate.First; s1:=''; s2:=''; while not Cdsupdate.Eof do begin Cmdstr:=''; case Cdsupdate.UpdateStatus of usUnmodified: //从原数据行取得修改条件 begin s2:=''; for j:=1 to Keylist.Count do begin if s2='' then s2:=Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]]) else s2:=s2+' and '+Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]]); end; end; usModified: begin s1:=''; for i:=1 to Cdsupdate.FieldCount do begin if (not Cdsupdate.Fields[i-1].isNull)and(Cdsupdate.Fields[i-1].tag=1) then begin if s1='' then s1:=Trim(Cdsupdate.Fields[i-1].FieldName)+' = '+vartosql(Cdsupdate.Fields[i-1].value) else s1:=s1+','+Trim(Cdsupdate.Fields[i-1].FieldName)+' = '+vartosql(Cdsupdate.Fields[i-1].value); end; end; if s1<>'' then Cmdstr:=' update '+ptablename+' set '+s1+' where '+s2; end; usInserted: begin s1:=''; s2:=''; for i:=1 to Cdsupdate.FieldCount do if (not Cdsupdate.Fields[i-1].isNull)and(Cdsupdate.Fields[i-1].tag=1) then begin if s1='' then begin s1:=Trim(Cdsupdate.Fields[i-1].FieldName); s2:=vartosql(Cdsupdate.Fields[i-1].value); end else begin s1:=s1+','+Trim(Cdsupdate.Fields[i-1].FieldName); s2:=s2+','+vartosql(Cdsupdate.Fields[i-1].value); end; end; if s1<>'' then Cmdstr:=' Insert into '+ptablename+'('+s1+') values('+s2+')'; end; usDeleted: begin s2:=''; for j:=1 to Keylist.Count do begin if s2='' then s2:=Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]]) else s2:=s2+' and '+Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]]); end; Cmdstr:='Delete '+ptablename+' where '+s2; end; end; if Cmdstr<>'' then sqlstr:=sqlstr+Cmdstr+';'+chr(13)+chr(10); Cdsupdate.Next; end; end; finally FreeAndNil(Cdsupdate); FreeAndNil(keylist); end; Result:=sqlstr;end;
 
怎么没有人回答呢?高手都哪儿去了?
 
好大一块砖.
 
把数据保存部分单独粘出来,请兄弟姐妹们帮忙看一下!function TXSFHOp.SaveData(BH:string):Boolean; var str:string; begin Result:=True; try DataMod.ADOConn.BeginTrans; //MasterData _BankBH:=GetBank_BH(_BankName); if BH='' then begin _BH:=GetBH; str:='insert into XSFH_Master(BH,kf,id,CKMC,ZDR,JBR,s_type,' +'S_date,SQ_money,SJ_money,BankBH,S_memo)values(' +Quotedstr(_BH)+','+Quotedstr(_KF)+','+Quotedstr(_ID) +','+Quotedstr(_CKMC)+','+Quotedstr(_ZDR)+','+Quotedstr(_JBR) +','+Quotedstr(_S_type)+','+Quotedstr(_S_Date)+','+floattostr(_SQ_money) +','+floattostr(_SJ_money)+','+Quotedstr(_BankBH) +','+Quotedstr(_S_Memo)+')' end else begin str:='update XSFH_Master set KF='+Quotedstr(_kf)+',' +'ID='+Quotedstr(_ID)+',' +'S_Date='+Quotedstr(_S_Date)+',' +'SQ_money='+floattostr(_SQ_money)+',' +'SJ_money='+floattostr(_SJ_money)+',' +'CKMC='+Quotedstr(_CKMC)+',' +'ZDR='+Quotedstr(_ZDR)+',' +'JBR='+Quotedstr(_JBR)+',' +'BankBH='+Quotedstr(_BankBH)+',' +'S_memo='+Quotedstr(_S_Memo) +' where BH='+Quotedstr(_BH) end; SQLExec(str); if _Detail.ChangeCount>0 then begin _Detail.First; while not _Detail.Eof do begin if _Detail.FieldByName('BH').AsString<>_BH then begin _Detail.Edit; _Detail.FieldByName('BH').AsString:=_BH; _Detail.Post; end; _Detail.Next; end; str:=(GetSqlS(DataMod.ADOConn,_Detail.Delta,'XSFH_Detail','ID')); SQLExec(str); //_Detail.MergeChangeLog; //清空Delta中的数据 _Detail.Data:=FindAllRec('Select * from XSFH_Detail where BH='''+_BH+''''); end; DataMod.ADOConn.CommitTrans; MessageBox(0, '添加成功!', '提示', MB_OK + MB_ICONINFORMATION); except DataMod.ADOConn.RollbackTrans; MessageBox(0, '添加失败!', '错误', MB_OK + MB_ICONWARNING); Result:=false; end; end;
 
我是高手,太长了没时间看。
 
讲点思想就可以了,代码没人有心思看
 
你把处理方法和出错现象说说,谁有时间一行一行代码去看呀?
 
贴子内容已修改,见最上面请高手们谈谈主从表的存储思想有没有比ClientDataSet好的方法?
 
我一般都是实时保存。另外,每个主从表都写一个这样的保存过程当然很麻烦,你可以想办法将保存过程过程化,这样有利于代码重用。
 
进来的朋友都说两句!!!
 
我是低手,看不懂问题,但感觉主从表存储没这么麻烦呀,一个master ds,几个detail ds就行了。单头先保存,明细再输一下。再改下单据状态,生效单据。没碰到过问题。
 
我一直都是ApplyUpdates(0)保存主从表,没的问题的,你是不主从关系没建立好
 
我不是高手,我不乱讲。我看看。
 
后退
顶部