BMS-HOSxP Community

HOSxP => Delphi / Pascal => ข้อความที่เริ่มโดย: ZemaplUS2 ที่ มิถุนายน 01, 2013, 22:23:31 PM

หัวข้อ: อยากทราบรูปแบบการเขียนโปรแกรมประมวลผลทีละ Reccord
เริ่มหัวข้อโดย: ZemaplUS2 ที่ มิถุนายน 01, 2013, 22:23:31 PM
procedure TForm1.Button1Click(Sender: TObject);
VAR
num : integer ;
count : integer ;
sex : string ;
begin
  num := 1 ;

  count := MyQuery1.RecordCount;
  ProgressBar1.Max := count;
  MyQuery1.First;
            while not MyQuery1.Eof do
            begin
              ProgressBar1.Position :=  ProgressBar1.Position + 1;
            //  Sleep(1);
              sex := myquery1.fieldbyname('sex').asstring;
            if sex = '1' then
              begin


              end;
            if sex = '2' then
              begin


              end;

              MyQuery1.Next;
            end;

end;
////////////////////////////////////
       จากรูปแบบที่เขียนด้านบน หากต้องการให้มีการกระทำ โดยมีการดึงข้อมูลทีละ Record มาเก็บในตัวแปรแล้วทำการเปรียบเทียบ โดยในโปรแกรมหากต้องการตรวจสอบว่าตัวแปร Sex มีค่าเป็น 1 ให้ไปเปลี่ยนค่าในฟิวส์ lname เป็น 1 ด้วย หากSex มีค่าเป็น 2 ให้ไปเปลี่ยนค่าในฟิวส์ lname เป็น 2 ไม่ทราบว่าจะมีรูปแบบการเขียนแบบใดครับ ขอบคุณครับ
หัวข้อ: Re: อยากทราบรูปแบบการเขียนโปรแกรมประมวลผลทีละ Reccord
เริ่มหัวข้อโดย: armds ที่ มิถุนายน 02, 2013, 00:24:18 AM
if ก็ทำได้นะ
...
if sex = '1' then
begin

end
else
begin

end;
...

หรือลองใช้ case ดูนะครับ

...

case strtoint(sex) of
     1 : begin
     
          end;
     2 : begin
     
          end;
     else begin
       
           end;
end;
.....
หัวข้อ: Re: อยากทราบรูปแบบการเขียนโปรแกรมประมวลผลทีละ Reccord
เริ่มหัวข้อโดย: ZemaplUS2 ที่ มิถุนายน 02, 2013, 11:03:03 AM
ได้คำตอบแล้วครับ เผื่อใครจะหาวิธีทำแบบนี้อยู่ครับ ให้ไว้เป็นแนวครับ
procedure TForm1.Button1Click(Sender: TObject);
VAR
num : integer ;
cid : string ;
count : integer ;
sex : string ;
begin
  num := 1 ;
  count := MyQuery1.RecordCount;
  ProgressBar1.Max := count;
  MyQuery1.First;

            while not MyQuery1.Eof do
            begin
              ProgressBar1.Position :=  ProgressBar1.Position + 1;
            //  Sleep(1);
              sex := myquery1.fieldbyname('sex').asstring;
              cid := myquery1.fieldbyname('cid').asstring;
            if sex = '1' then
              begin
              MyQuery2.Open;
              MyQuery2.Edit;
              MyQuery2.SQLUpdate.Text := 'Update person set lname = "1" where cid = '+'"'+cid+'"';
              MyQuery2.Post;
              end;
            if sex = '2' then
              begin
              MyQuery2.Open;
              MyQuery2.Edit;
              MyQuery2.SQLUpdate.Text := 'Update person set lname = "2" where cid = '+'"'+cid+'"';
              MyQuery2.Post;

              end;

              MyQuery1.Next;
            end;

end;
หัวข้อ: Re: อยากทราบรูปแบบการเขียนโปรแกรมประมวลผลทีละ Reccord
เริ่มหัวข้อโดย: manoi ที่ มิถุนายน 03, 2013, 11:48:18 AM
ได้คำตอบแล้วครับ เผื่อใครจะหาวิธีทำแบบนี้อยู่ครับ ให้ไว้เป็นแนวครับ
procedure TForm1.Button1Click(Sender: TObject);
VAR
num : integer ;
cid : string ;
count : integer ;
sex : string ;
begin
  num := 1 ;
  count := MyQuery1.RecordCount;
  ProgressBar1.Max := count;
  MyQuery1.First;

            while not MyQuery1.Eof do
            begin
              ProgressBar1.Position :=  ProgressBar1.Position + 1;
            //  Sleep(1);
              sex := myquery1.fieldbyname('sex').asstring;
              cid := myquery1.fieldbyname('cid').asstring;
            if sex = '1' then
              begin
              MyQuery2.Open;
              MyQuery2.Edit;
              MyQuery2.SQLUpdate.Text := 'Update person set lname = "1" where cid = '+'"'+cid+'"';
              MyQuery2.Post;
              end;
            if sex = '2' then
              begin
              MyQuery2.Open;
              MyQuery2.Edit;
              MyQuery2.SQLUpdate.Text := 'Update person set lname = "2" where cid = '+'"'+cid+'"';
              MyQuery2.Post;

              end;

              MyQuery1.Next;
            end;

end;


ไม่ควรเขียนแบบนี้ครับ ควรเขียนแบบนี้

myquery2.sql.text:='select * from patient where cid = '+QuotedStr(cid);
myquery2.open;
while not myquery2.eof do
begin
  myquery2.edit;
  if myquery2.fieldbyname('sex').asstring = '1' then
  myquery2.fieldbyname('lname').asstring:='1' else
  if myquery2.fieldbyname('sex').asstring = '2' then
  myquery2.fieldbyname('lname').asstring:='2' ;
  myquery2.post;
  myquery2.next;
end;
myquery2.close;