2016-09-30 104 views
-3
with dmHospital do 
    begin 
    qryHospital.SQL.Clear; 
    qryHospital.SQL.Add('SELECT * FROM Patients ') ; 
    qryHospital.SQL.Add('WHERE DoctorID = :DoctorID'); 
    qryHospital.Parameters.ParamByName('DoctorID').Value := StrToInt(sID); 
    qryHospital.Open; 
    iCount := qryHospital.RecordCount 
end; 

此代码显示我想要放入数组中的值。但是我不确定如何遍历每条记录,并将记录中的每个值都存入正确的数组中。例如:我想要'PatientName'中的姓名和'PatientSurname'中的姓氏。 iCount是数组大小。如何使用SQL将表中的插入值插入到数组中?

+0

你到目前为止尝试过什么?什么是“dmHospital”,“qryHospital”,我们应该如何知道您的SQL数据库的外观如何? – Wosi

+0

'而Query.Eof Query.Next' –

+0

在联机帮助中查找TDataSet.First,.Next和.Eof。或者只是谷歌他们。 – MartynA

回答

1

您必须使用WhileTDataset开始循环。

一些示例代码:

... 
var 
    fieldCod:TField; 
    Str1:String; 
    i, Cod:Integer; 
begin 
    ... 
    qryHospital.Open; 
    iCount := qryHospital.RecordCount; 
    // Create pointer to field 
    fieldCod := qryHospital.FieldByName('PatientCode'); 
    // loop the recordset (while not arrive at end) 
    While (not qryHospital.eof) do begin 
    // Different modes to access table fields content 
    Str1 := qryHospital.FieldByName('PatientName').AsString; 
    i := qryHospital.Fields[1].AsInteger; 
    Cod := fieldCod.AsInteger;  

    // Add the values to your array 
    //... 

    // Next Record 
    qryHospital.Next; 
    end; 

注意:为了获得更好的性能,请不要使用FieldByName内环路(大对数的记录)。您可以使用Fields[index]或在循环外创建一个变量并引用字段。

+0

@germán-estévez-neftalí谢谢! –

+1

一个不应该在循环内使用FieldByName - 它很慢! 创建一堆TField类型的本地变量并在循环之前使用FieldByName填充它们///同样使用RecordCount对于SQL来说确实是不好和风险的样式 –

+1

@Arioch'The。 Goog折旧。编辑帖子.... –