0

I need to import data from Excel into a SQL Server database. I use ADO to read the excel files.

Sometimes it happens that a row is empty in Excel, which will create an empty row import failure on the SQL Server side.

Any good idea to remove these empty rows or detect during import?

I'm looking for a rather effective code style solution, i show my current solution with the field loop here

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;
1
  • 2
    If you want help with this, add the code you're using to your q. You can't reasonably expect readers to guess what you're doing. Commented Nov 24, 2015 at 18:39

1 Answer 1

2

You could exit the first thime you find a non empty string

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

  exit(True);
end;

And if you have an older Delphi you could implement it like 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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.