2012-07-21 60 views
1

我一直负责修补Delphi软件以使用新的数据库(mssql)结构。 在以前的数据库(mssql)中,所有正在读取的列位于同一个表中。 在新版本中,file_name和class_name与数量值位于不同的表中。 我相信我可以通过为这两个数据库表写入Join来解决这个问题。 问题是,我不熟悉德尔福!
当前的代码如下。 如何将这两个数据库合并为一个?德尔菲表加入

的dataTable = style001 dataTable1 =风格

谢谢!

dataTable.Active := True; 
    datatable.Open; 
    dataTable1.Active := True; 
    datatable1.Open; 

    while not datatable1.Eof do 
    begin 
    Application.ProcessMessages; 

    file_name := trim(datatable1.FieldByName('code').asString) + '.jpg'; 
    class_name := trim(datatable1.FieldByName('category').asString); 

    if not FileExists(picfolder + file_name) then 
    begin 
     dataTable1.next; 
     continue; 
    end; 

    instock := datatable.FieldByName('onhand').asString; 
    TryStrToInt(instock, instock_num); 

    quantity := datatable.FieldByName('onorder').asString; 
    TryStrToInt(quantity, num); 

    num := instock_num - num; 

    is_active := (num > 10); 


    for i := 0 to file_count-1 do 
    begin 
     if is_active = active_class[file_class[i]] then 
     if SameText(files[i],file_name) then 
     begin // use TStringList instead 
     if SameText(class_name, classes[file_class[i]]) then // same class 
     begin 
      file_ok[i] := true; 
      class_ok[file_class[i]] := true; 
     end; 
     break; 
     end; 
    end; 

    if class_name <> '' then class_name := class_name + '\'; 
    dest := picfolder + active_path[is_active] + class_name; 
    deldest := picfolder + active_path[not is_active] + class_name; 

    {$I-} 

    if FileExists(deldest + file_name) then 
     DeleteFile(PChar(deldest + file_name)); 

    if not FileExists(dest + file_name) then 
    begin 
     ForceDirectories(dest); 
     CopyFile(PChar(picfolder + file_name), PChar(dest + file_name), true); 
    end; 

    dataTable1.next; 
    end; 

    for i := 0 to file_count-1 do 
    if not file_ok[i] then 
    begin // delete wrong file if empty 
//  ShowMessage('problem: ' + picfolder + active_path[active_class[file_class[i]]] + classes[file_class[i]] + '\' + files[i]); 
     DeleteFile(picfolder + active_path[active_class[file_class[i]]] + classes[file_class[i]] + '\' + files[i]); 
    end; 
    for i := 0 to class_count-1 do 
    if not class_ok[i] then 
    begin // delete old class if empty 
//  ShowMessage('problem: ' + picfolder + active_path[active_class[i]] + classes[i] + '\'); 
     RemoveDir(picfolder + active_path[active_class[i]] + classes[i] + '\'); 
    end; 


    beep; 
    dataTable.Active := False; 
    dataTable1.Active := False; 

回答

4

联接SQL请求,不是你在这里展示的消费循环代码中。

您必须使用TQuery而不是两个TTable,然后编写一个SQL select来连接这两个表。

您必须修改SQL请求,在SELECT的FROM子句中添加两个表,并在WHERE子句中添加一个JOINture。见this article about JOIN

+0

研究。谢谢。 – pythondjango 2012-07-21 21:27:02

2

使用TDataSource并将其DataSet属性连接到其中一个表。然后将另一个表的MasterSource属性设置为该数据源。单击MasterFields属性的省略号按钮,然后选择连接字段。

假设风格字段是唯一的,当您浏览第一个表格时,第二个表格会自动跟随。

+0

这听起来正是我所需要的。尝试。 – pythondjango 2012-07-21 22:14:51