2015-11-24 198 views
0

我需要从Excel导入数据到SQL Server数据库。我使用ADO来读取excel文件。从Excel导入数据到SQL Server

有时会发生Excel中一行为空的情况,这会在SQL Server端创建空行导入失败。

任何好主意删除这些空行或在导入过程中检测?

我正在寻找一个相当有效的代码风格的解决方案,我会让我与这里的场回路电流解决方案

function EmptyRow(aQuery: TADOQuery): Boolean; 
var 
    i: Integer; 
    fname: string; 
    temp_line: string; 
begin 

    temp_line := ''; 
    for i := 0 to aQuery.Fields.Count - 1 do 
    begin 
    fname := aQuery.Fields[i].FieldName; 
    temp_line := temp_line + aQuery.FieldByName(fname).asString; 
    end; 
    if (temp_line <> '') then 
    result := false 
    else 
    result := true; 
end; 
+2

如果你想在这方面帮助,一dd你正在使用的代码到你的q。你不能合理地期待读者猜测你在做什么。 – MartynA

回答

2

你可以退出第一thime你找到一个非空字符串

function EmptyRow(aQuery: TADOQuery): Boolean; 
var 
    Field: TField; 
begin 
    for Field in aQuery.Fields do 
    if Field.AsString <> '' then 
     exit(false); 

    exit(True); 
end; 

如果你有一个旧的德尔福,你可以实现它像tihs:

function EmptyRow(aQuery: TADOQuery): Boolean; 
var 
    I: Integer; 
begin 
    Result := False; 
    for I := 0 to aQuery.Fields.Count - 1 do 
    if aQuery.Fields[I].AsString <> '' then 
     exit; 

    Result := True; 
end; 
相关问题