Delphi 判斷ClientDataSet控件的UpdateStatus屬性類型爲 (usUnmodified, usModified, usInserted, usDeleted))

 

Delphi 判斷ClientDataSet控件的UpdateStatus屬性類型爲 (usUnmodified,   usModified, usInserted,  usDeleted))

       

        根據ClientDataSet控件的UpdateStatus屬性類型爲(usUnmodified, usModified, usInserted, usDeleted),解析並拼成相應的增、刪和改操作語句:

 

實例如下:
// ATableName : 將要修改的表名

// APrimaryKey : 表的主鍵值

// AIsMulti : 數據中包含的表的個數

// ADelta : 更新的數據集

procedure TForm1.Save(const ATableName, APrimaryKey, AIsMulti: WideString;

  ADelta: OleVariant; var ErrorMsg: WideString);

var

  ActualFieldList: TStringList;

  PrimaryFieldList: TStringList;

 

  i: Integer;

  FieldNameList: string; //字段名稱列表

  FieldValueList: string; //字段值列表

  FieldNameValueList: string; //字段值=名稱列表

  SetValueList: string; //Set字段值=名稱列表,   update的Sql用

  SqlStr: string;

 

    //返回欲修改表的字段名稱列表

  procedure GetFieldNameList(const ATableName: string);

  var

    QueryStr: string;

  begin

    QueryStr := 'select   *   from   ' + ATableName + '   where   1   =   0 ';

    try

      FClientDataSet.Data := FDBoperation.cdsGetDataForRead(QueryStr, ErrorMsg);

    except

      Exit;

    end;

    FClientDataSet.GetFieldNames(ActualFieldList);

  end;

 

  //返回欲修改表的   主鍵字段名稱=值串

  function GetPKValueList: string;

  var

    j: Integer;

    ValueStr: string;

    ResultStr: string;

  begin

    ResultStr := ' ';

    for j := 0 to PrimaryFieldList.Count - 1 do

    begin

      with FClientDataSet do

      begin

        case FieldByName(PrimaryFieldList[j]).DataType of

          ftString, ftDate, ftDateTime:

            ValueStr := QuotedStr(FieldByName(PrimaryFieldList[j]).AsString);

          ftInteger:

            ValueStr := FieldByName(PrimaryFieldList[j]).AsString;

        end;

      end;

      if j > 0 then

        ResultStr := ResultStr + '   and   ';

      ResultStr := ResultStr + PrimaryFieldList[j] + '   =   ' + ValueStr;

    end;

    Result := ResultStr;

  end;

 

begin

  if AIsMulti = '1 ' then //Delta中僅包含一個表的數據

  begin

    FDBoperation.cdsUpdateDate(ATableName, ADelta, ErrorMsg);

  end

  else

  begin //Delta中包含多個表的數據

    ActualFieldList := TStringList.Create;

    PrimaryFieldList := TStringList.Create;

 

    try

            {===   得到欲修改表的字段名稱列表   ===}

      GetFieldNameList(ATableName);

 

      FClientDataSet.Data := ADelta;

      FClientDataSet.First;

      with FClientDataSet do

      begin

        while not Eof do

        begin

          case UpdateStatus of

            usModified:

              begin

                SetValueList := '   Set   ';

                for i := 0 to FieldCount - 1 do

                begin

                                //是欲修改表的字段之一

                  if ActualFieldList.IndexOf(Fields[i].FieldName) > -1 then

                  begin

                    case Fields[i].DataType of

                      ftString, ftDate, ftDateTime:

                        begin

                          if not Fields[i].IsNull then

                            SetValueList := SetValueList + Fields[i].FieldName

                              + '= ' + QuotedStr(Fields[i].AsString) + ', ';

                        end;

                      ftInteger:

                        begin

                          if not Fields[i].IsNull then

                            SetValueList := SetValueList + Fields[i].FieldName

                              + '= ' + Fields[i].AsString + ', ';

                        end;

                    end;

                  end;

                end;

                System.Delete(SetValueList, Length(SetValueList), 1);

                SqlStr := 'update   ' + ATableName + SetValueList + '   where   ' + FieldNameValueList;

              end;

            usInserted:

              begin

                FieldNameList := ' ';

                FieldValueList := ' ';

                for i := 0 to FieldCount - 1 do

                begin

                                //是欲修改表的字段之一

                  if ActualFieldList.IndexOf(Fields[i].FieldName) > -1 then

                  begin

                    FieldNameList := FieldNameList + Fields[i].FieldName + ', ';

                    if Fields[i].IsNull then

                      FieldValueList := FieldValueList + '   null, '

                    else begin

                      case Fields[i].DataType of

                        ftString, ftDate:

                          begin

                            FieldValueList := FieldValueList + QuotedStr(Fields[i].AsString) + ', ';

                          end;

                        ftInteger:

                          begin

                            FieldValueList := FieldValueList + Fields[i].AsString + ', ';

                          end;

                      end;

                    end;

                  end;

                end;

                            //刪除末尾的逗號

                System.Delete(FieldNameList, Length(FieldNameList), 1);

                System.Delete(FieldValueList, Length(FieldValueList), 1);

                SqlStr := 'insert   into   ' + ATableName + '( ' + FieldNameList + ') '

                  + '   values   ( ' + FieldValueList + ') ';

              end;

            usUnmodified, usDeleted:

              begin

                            {===   得到欲修改表的主鍵字段名稱列表   ===}

                PrimaryFieldList.CommaText := APrimaryKey;

                            {===   得到欲修改表的主鍵字段名稱=值串   ===}

                FieldNameValueList := GetPKValueList;

                if UpdateStatus = usDeleted then

                  SqlStr := 'delete   from   ' + ATableName + '   where   ' + FieldNameValueList;

              end;

          end;

          if UpdateStatus <> usUnmodified then

          begin

            try

              FDBoperation.ExecSql(SqlStr, ErrorMsg);

            except

              raise;

            end;

          end;

          Next;

        end;

      end;

    finally

      ActualFieldList.Free;

      PrimaryFieldList.Free;

    end;

  end;

end;

發佈了72 篇原創文章 · 獲贊 13 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章