1

What is the simplest way to execute a SQL query to a SQL Server 2005, remotely, using Borland Delphi?

In this case, I am using Borland Delphi 7.

The simplest way means, if I must use 3rd party component, it should be integrated in the application (not separate/complicated installation).

The thing is, I want to be able to check if the remote SQL server is alive.

I know, we can use network "ping", but that does not mean the SQL server is fully accessible and functional.

Thanks in advance for any tips :)

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
Kawaii-Hachii
  • 1,017
  • 7
  • 22
  • 36

1 Answers1

3

Simplest

  1. Drop a TADOQuerycomponent on your form.
  2. Fill out the connectionstring property (or use the ... build button).
  3. Put your SQL Statement in the SQL property.
  4. Set your components Active property to True.

Better

  1. Use a TDatamodule.
  2. Use a TADOConnection component.
  3. Link your TADOQuery component to the TADOConnection component.
  4. Activate your query in runtime when needed io in Designtime. Active Connections or Queries in Designtime usually end up being a waste of CPU cycles when running your application.

Console Application

program SimpleSQL;

{$APPTYPE CONSOLE}

uses
  ADODB,
  SysUtils;

var
  qry: TADOQuery;
begin
  qry := TADOQuery.Create(nil);
  try
    qry.ConnectionString := 'AConnectionString';

    // Update
    qry.SQL.Text := 'UPDATE YourTable SET FieldX = FieldY';
    qry.ExecSQL;

    // Select
    qry.SQL.Text := 'SELECT FieldX FROM dbo.YourTable';
    qry.Open;
    qry.First;
    while not qry.Eof do
    begin
      Output(qry.Fields[0].AsString);
      qry.Next;
    end;
    qry.Close;    

  finally
    qry.Free;
  end;
end.
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146