【问题标题】:How do I check input data from edits that will be posted to a database with delphi?如何检查将使用 delphi 发布到数据库的编辑中的输入数据?
【发布时间】:2013-07-18 14:27:05
【问题描述】:

我用 Delphi7 编写了一个数据库应用程序。我正在使用 ADODatabase。我正在使用编辑从用户那里获取输入,但程序不起作用,它检查输入数据但从不发布它。我有三个表名为 Clients Receivers 和 Packages,请帮助我。代码如下:

 'procedure TNewDel.Button1Click(Sender: TObject);
  Var
 Name, Name2,LName,LName2, SMessage,Date, Date2, ClNum,PackNum, RcNum, email, email2,  address, address2, cell, Cell2, Cellnum, Cellnum2, Price  :string;
k, c, c2,CountNum, weight, Distance, size :integer;
rPrice:real;
cCheck:char;
begin

            Date:= FloatToStr(DateTP.Date);
            CountNum:= 0;
            CountNum:=CountNum + 1;
    Name:= edtName.text;
    LName:= edtLName.text;
    SMessage:= ' Wrong input: ';
    Address:= edtAdd.Text;
    Email:= edtEmail.Text;
    cell:= edtCellnum.Text;

    Name2:=edtName2.text;                                     
    LNAme2:=edtLName.text;
    CellNum2:=edtCellNum.text;
    Address2:= edtAdd2.text;
    email2:= edtemail2.text;
    Cell2:= edtCellNum2.text;

    Weight:=sedWeight.value;
    Distance:= sedDist.value;
    Size:= sedSize.Value;



For K := 1 to 4       do                                                                              
 Begin
  Date2:= Date2+date[k];
 End;

 If LName= '' then  
      Begin                                                                       
         ShowMessage(sMessage+ 'Last Name');
        End
 Else


 Begin
If Name= '' then
    begin
         ShowMessage(sMessage+ 'Name');
    End

Else


    Begin
If Address= '' then
    Begin
         ShowMessage(sMessage+ 'Address');
    End

Else
 Begin
If Email= '' then
    Begin
     ShowMessage(sMessage+ 'E-Mail');
End

 Else
    begin   

If Cell = '' then
     Begin
      ShowMessage(sMessage+ 'Cell Phone Number');
            End

    Else    

 Begin

If Cell2 = '' then
    Begin
     Showmessage(sMessage + 'Receiver Cell Number');
        End

Else
 Begin      
 If Name2 = '' then
    Begin
     Showmessage(sMessage + 'Receiver Name');
    End

Else
 Begin      

If LName2 = '' then
    Begin
     Showmessage(sMessage + 'Receiver Last Name');
    End 

Else
 Begin      

If Address2 = '' then
    BEGIN
     Showmessage(sMessage + 'Receiver Address');
    End

Else
Begin       

If Email2 = '' then
    Begin
     Showmessage(sMessage + 'Receiver Cell Number');
    End

Else
 Begin  


If Distance='' then
    Begin
     ShowMessage(sMessage+ 'Distance');
    End

Else
 Begin      
If Size='' then
    begin 
     ShowMessage(sMessage+ 'Size');
    End

Else
Begin       

If Weight > 35 Then


Begin
   Showmessage('Package too heavy');
end
  Else

        Begin
    If Distance <= 150 then
        begin
         rPrice:= Distance*3.5;
         Price:= FloatToStr(rPrice);
        End

               Else
    Begin
        If distance >150 then
            Begin
             rPrice:= (150*3.5)+((distance-150)*3.5*2);
             Price:=FloatToStr(rPrice);
            End

    Else
Begin 
    If distance = 0 then
    Begin
    ShowMessage(sMessage + 'Distance');
    END

Else
Begin
CCheck:= 't';
end;
 End;


If CCheck = 't' then
Begin
        For c:= 2 to 10 do
                    Begin
                       CellNum:= Cellnum+ Cell[c];
                      End;
                        CellNum:= '+27' + Cellnum;
        For c2:= 2 to 10 do
            Begin
             CellNum2:= Cellnum2 +Cell2[c2];
            end;
     CellNum2:= '+27' + CellNum2 ;

ClNum:= 'CL'+ Name[1]+LName[1]  + Date2 + IntToStr(CountNum);
PackNum:='PK' + Name[1]+Name2[1]+ Date2;
RcNum:= 'RC'+ Name2[1]+Lname2[1]+ Date2;


ADOTable1.edit;
  ADOtable1Package_Number.Value:= PackNum;
  ADOTable1client_Number.Value:=ClNum;
  ADOTable1Name:=Name;
  ADOTable1Last_Name.Value:= LName;
  ADOTable1Address.Value:=Address;
  ADOTable1e_mail.Value:=email;
  ADOTable1Cell_Number.Value:=CellNUm;
  ADOTable1Date.Value:=Date;
Post;
End;

With ADOTable2 do
Begin
 Insert;
  FieldByName('Package_Number').Value:= PackNum;
  FieldByName('Receiver_Number').Value:=RCNum;
  FieldByName('Name').Value:=Name2;
  FieldByName('Last_Name').Value:=LNAme2;
  FieldByName('Address').Value:=Address2;
  FieldByName('E_mail').Value:=email2;
  FieldByName('Date').Value:=Date;
Post;
End;


with ADOTable3 do
    begin
     Insert;
      FieldByname('Price').Value:= Price;
      FieldByName('Package_Number').Value:= PackNum;
      FieldByName('Client_Number').Value:= ClNum;
      FieldByName('Receiver_Number').Value:=RcNum;
  FieldByName('Weight').Value:= Weight;
  FieldByName('Size').Value:=Size;
  FieldByName('Price').Value:= Price;
  FieldByName('Distance').Value:= Distance;
Post;
    end;

end;

end;
end;
 end;
 end;
  end;
end;
  end;
    end;
      end;
        end;
          end;
            end;
              end;
                end;
                  end;

我知道这可能不是最好的方法,我是一个大人物,所以这就是我问的原因。谢谢。

【问题讨论】:

  • 这个几乎无限嵌套的IF 无法阅读。通常你应该将这样的代码炸弹重构为小程序。希望这会带来更多的经验。但是对于初学者来说,请将该过程重新设计为小的非嵌套序列,作为这两种模式之一:pastebin.com/8AGGkidw - 目前不仅您几乎无法解析它,而且对其他人来说也很难。 PS:只是建议新手更快地获得答案:catb.org/esr/faqs/smart-questions.html
  • 如果你正确缩进你的代码,你会看到,如果你输入If Distance &lt;= 150 then,你将永远不会在帖子中输入ELSE部分。
  • @Bummi 你是个怪物 :-D 但是为什么不让我回答呢? PS。但在这种情况下最好的缩进 - 根本没有缩进(也没有嵌套)。 PS。到主题启动器 - 避免使用像“Button1”这样的名称 - 给它一个合理的名称。仅仅在一个月内,如果不进行冗长乏味的阅读,您将无法理解Button1ClickButton2Click 之间的区别。不要养成这个坏习惯 - 这是一个耗时的吃脑沼泽。
  • 是否有充分的理由避免使用数据件组件?他们会让大部分代码消失。
  • @AlexSC 也许他不希望在编辑过程中并且仅在发布之前显示错误。虽然可以使用可视化验证器标记组件

标签: delphi delphi-7 ado


【解决方案1】:

我做了类似的事情

function CheckValues(const aValue, aConst: Array of string): string;
var
  a_Index: integer;
  a_Const: String;
begin
  Result := '';
  Assert(High(aValue)= High(aConst), 'Programmer Error CheckValues: Value and Const should have the same amount of elements');
  for a_Index := Low(aValue) to High(aValue) do
  begin
    if (aValue[a_Index] = '') then
    begin
      a_Const := aConst[a_Index];
      if Result = '' then
        Result := a_Const
      else
        Result := Result + ', ' +  a_Const;
    end;
  end;
end;


procedure TForm.AllowSave;
var
  a_Value: string;
begin
  if FInit then
  begin
    a_Value := CheckValues([cbChain.Text, edName.Text, edTaxId.Text, edCustomerNo.Text, ReversalWindow.Text, editAddress1.Text,
    editCity.Text, cbState.Text, editZip.Text, editPhone.Text, editFax.Text, editEmail.Text, cbPricingTypeLU.Text],
   ['Chain','Name',  'Tax Id', 'Customer Number', 'Reversal Window', 'Address1',
    'City', 'State', 'Zip', 'Phone', 'Fax', 'Email','Pricing Type']);


    btnSave.Enabled := a_Value = '';
    if btnSave.Enabled then
    begin
      StatusBar1.SimpleText := '';
      if not (qryEdit.State in [dsInsert, dsEdit]) then
        qryEdit.Edit;
    end else
      StatusBar1.SimpleText := 'Missing: ' + a_Value;
  end;
end;

【讨论】:

    【解决方案2】:

    数据库应用程序编码的一些技巧(考虑你是初学者):

    1. 最好使用 DB 感知控件。 (例如:使用 TDBEdit 而不是 TEdit)
    2. 对于数据验证,请使用事件处理程序。

    在你的情况下,你可以这样做:

    1. 使用数据库感知控件。
    2. 处理所有 TADOTable 实例的 OnBeforePost 事件

      procedure TForm1.ADOTable1BeforePost(DataSet: TDataSet);

      begin

      if SameText(ADOTable1Last_Name.AsString, EmptyStr) then

      begin

       Showmessage(sMessage + 'Receiver Last Name');
      
       Abort;//here you can also set the focus back to the ADOTable1Last_Name control.
      

      end;

      end;

    您可以逐渐添加所有验证。通过这样做,如果您进一步对控件进行任何更改,您将永远不需要更改验证代码。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-11-23
      • 2018-01-02
      • 2023-03-17
      • 2019-12-29
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      相关资源
      最近更新 更多