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;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi中,DBGridEh是一个非常常用的数据表格控件,它可以用于显示和编辑数据库中的数据。而LookupDisplay是DBGridEh中的一个属性,用于提供表格中关联表字段的显示值。 通常情况下,DBGridEh的LookupDisplay属性用于显示关联字段的显示值,该值将从关联表中获取。在使用DBGridEh时,需要使用ClientDataSet组件来提供数据。ClientDataSet是一种内存数据集,可以在客户端应用程序中存储和处理数据。 要在DBGridEh中使用LookupDisplay属性,首先需要在ClientDataSet中定义关联字段。关联字段是指与主表的某个字段关联的从表的字段。在ClientDataSet的字段编辑器中,可以通过设置LookupDataset、LookupKeyFields和LookupResultField属性来定义关联字段。 LookupDataset属性用于指定从表所在的ClientDataSet组件,LookupKeyFields属性是用来指定主表与从表关联的字段名,LookupResultField属性则是用来指定从表中要显示的字段名。 然后,在DBGridEh的Columns属性中找到需要显示关联字段的列,将这些列的LookupDisplay属性设置为True。这样,DBGridEh将会自动从关联表中获取相应的显示值,并在表格中显示出来。 总的来说,使用Delphi中的DBGridEh和ClientDataSet组件,可以很方便地实现表格中两列的LookupDisplay功能,即从关联表中获取显示值并显示在表格中,提供更友好的用户界面和数据交互体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值