0% found this document useful (0 votes)
1K views3 pages

ADOQuery (Delphi) - RAD Studio Code Examples

This document provides an example of using ADO for database connectivity in Delphi. It demonstrates creating an ADO connection to a SQL Server database, executing a prepared SQL query to retrieve data from a customer table, and displaying the results in a DBGrid component. The code connects to the database, defines the SQL query with a parameter, executes the query to get customer data, populates the grid, and provides error handling.

Uploaded by

Nizar Haddad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views3 pages

ADOQuery (Delphi) - RAD Studio Code Examples

This document provides an example of using ADO for database connectivity in Delphi. It demonstrates creating an ADO connection to a SQL Server database, executing a prepared SQL query to retrieve data from a customer table, and displaying the results in a DBGrid component. The code connects to the database, defines the SQL query with a parameter, executes the query to get customer data, populates the grid, and provides error handling.

Uploaded by

Nizar Haddad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ADOQuery (Delphi) - RAD Studio Code Examples

1 dari 3

http://docwiki.embarcadero.com/CodeExamples/XE7/en/ADOQuery_...

ADOQuery (Delphi)
From RAD Studio Code Examples

Language:
Delphi

Contents
1 Description
2 Code
3 Uses
3.1 See Also

Description
This example demostrates the use of ADO for database conectivity. The example assumes that a TDBGrid is
placed on the form.

Code
procedure TForm2.FormCreate(Sender: TObject);
const
{ Connection string }
ConnString =
'Provider=SQLOLEDB.1;Persist Security Info=False;' +
'User ID=%s;Password=%s;Data Source=%s;Use Procedure for Prepare=1;' +
'Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;'+
'Tag with column collation when possible=False';
{ SQL Query }
SQLStr = 'SELECT * FROM customer WHERE customer_id = :AnId;';
{ User access }
UserName = 'db_user_name';
PassWord = 'db_pass_word';
Server = 'my.db.server';
var
ADOConn
ADOQuery
DataSrc
Param

:
:
:
:

TADOConnection;
TADOQuery;
TDataSource;
TParameter;

begin
{ Create an ADO connection. }
ADOConn := TADOConnection.Create(Self);
{ Set up the provider engine }

20-11-2014 15:00

ADOQuery (Delphi) - RAD Studio Code Examples

2 dari 3

http://docwiki.embarcadero.com/CodeExamples/XE7/en/ADOQuery_...

{ Set up the connection string. }


ADOConn.ConnectionString := Format(ConnString,
[UserName, PassWord, Server]);
{ Disable login prompt. }
ADOConn.LoginPrompt := False;
try
ADOConn.Connected := True;
except
on e: EADOError do
begin
MessageDlg('Error while connecting', mtError,
[mbOK], 0);
Exit;
end;
end;
{ Create the query. }
ADOQuery := TADOQuery.Create(Self);
ADOQuery.Connection := ADOConn;
ADOQuery.SQL.Add(SQLStr);
{ Update the parameter that was parsed from the SQL query: AnId. }
Param := ADOQuery.Parameters.ParamByName('AnId');
Param.DataType := ftInteger;
Param.Value := 1;
{ Set the query to Prepared--it will improve performance. }
ADOQuery.Prepared := true;
try
ADOQuery.Active := True;
except
on e: EADOError do
begin
MessageDlg('Error while doing query', mtError,
[mbOK], 0);
Exit;
end;
end;
{ Create the data source. }
DataSrc := TDataSource.Create(Self);
DataSrc.DataSet := ADOQuery;
DataSrc.Enabled := true;
{ Finally, initialize the grid. }
DBGrid1.DataSource := DataSrc;
end;

Uses
Data.Win.ADODB.TADOConnection ( fr | de | ja )
Data.Win.ADODB.TADOQuery ( fr | de | ja )
Data.Win.ADODB.TParameter ( fr | de | ja )
Data.Win.ADODB.TADOConnection.ConnectionString ( fr | de | ja )
Data.DB.TCustomConnection.LoginPrompt ( fr | de | ja )
Data.DB.TCustomConnection.Connected ( fr | de | ja )

20-11-2014 15:00

ADOQuery (Delphi) - RAD Studio Code Examples

3 dari 3

http://docwiki.embarcadero.com/CodeExamples/XE7/en/ADOQuery_...

Data.Win.ADODB.TCustomADODataSet.Parameters ( fr | de | ja )
Data.Win.ADODB.TCustomADODataSet.Connection ( fr | de | ja )
Data.Win.ADODB.TADOQuery.SQL ( fr | de | ja )
Data.Win.ADODB.TCustomADODataSet.Prepared ( fr | de | ja )

See Also
Connecting to a Data Store Using TADOConnection
Inspecting the Update Status of Individual Rows
Retrieved from "http://docwiki.embarcadero.com/CodeExamples/XE7/e
/index.php?title=ADOQuery_(Delphi)&oldid=22779"
Categories: Delphi 2010
This page was last modified on 3 November 2011, at 12:05.
Help Feedback

20-11-2014 15:00

You might also like