ผู้เขียน หัวข้อ: อยากทราบรูปแบบการเขียนโปรแกรมประมวลผลทีละ Reccord  (อ่าน 5376 ครั้ง)

0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

ออฟไลน์ ZemaplUS2

  • Full Member
  • ***
  • กระทู้: 122
  • Respect: 0
    • ดูรายละเอียด
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 ไม่ทราบว่าจะมีรูปแบบการเขียนแบบใดครับ ขอบคุณครับ
Mainserver IBM 3100
Slave  PC ร้ายๆ ตัวหนึ่ง

ออฟไลน์ armds

  • Hero Member
  • *****
  • กระทู้: 1,084
  • armds
  • Respect: +10
    • ดูรายละเอียด
Re: อยากทราบรูปแบบการเขียนโปรแกรมประมวลผลทีละ Reccord
« ตอบกลับ #1 เมื่อ: มิถุนายน 02, 2013, 00:24:18 AM »
0
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;
.....
โรงพยาบาลสมเด็จพระยุพราชด่านซ้าย จ.เลย
ขนาด 60 เตียง นวก. คอมพิวเตอร์ ขึ้นระบบ ปี 2548
ขอบคุณ อ.ชัยพร อ.สุชัย อ.เดชา อ.doreamon อ.naj อ.ขวด และอ.ในเว็บ hosxp.net ทุกท่าน

ออฟไลน์ ZemaplUS2

  • Full Member
  • ***
  • กระทู้: 122
  • Respect: 0
    • ดูรายละเอียด
Re: อยากทราบรูปแบบการเขียนโปรแกรมประมวลผลทีละ Reccord
« ตอบกลับ #2 เมื่อ: มิถุนายน 02, 2013, 11:03:03 AM »
0
ได้คำตอบแล้วครับ เผื่อใครจะหาวิธีทำแบบนี้อยู่ครับ ให้ไว้เป็นแนวครับ
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;
« แก้ไขครั้งสุดท้าย: มิถุนายน 03, 2013, 00:15:53 AM โดย ZemaplUS2 »
Mainserver IBM 3100
Slave  PC ร้ายๆ ตัวหนึ่ง

ออฟไลน์ manoi

  • Hero Member
  • *****
  • กระทู้: 8,669
  • Respect: +170
    • ดูรายละเอียด
    • HOSxP Community Center
Re: อยากทราบรูปแบบการเขียนโปรแกรมประมวลผลทีละ Reccord
« ตอบกลับ #3 เมื่อ: มิถุนายน 03, 2013, 11:48:18 AM »
0
ได้คำตอบแล้วครับ เผื่อใครจะหาวิธีทำแบบนี้อยู่ครับ ให้ไว้เป็นแนวครับ
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;
HOSxP Project Manager / Cheif Developer / BMS MD