Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Learn How Easy It Is To Connect To Microsoft SQL Server In Windows Delphi Development

This sample allows the user to create a temporary connection definition at run-time and the master-details relationship between datasets using TFDQuery to work with MSSQL databases.

Location

You can find the MSSQL sample project at:

How to Use the Sample

  1. Navigate to the location given above and open GettingStarted.dproj.
  2. Press F9 or choose Run > Run.

Files

File in DelphiContains
GettingStarted.dproj
GettingStarted.dpr
The project itself.
fGettingStarted.pas
fGettingStarted.fmx
The main form.

Implementation

The sample implements the following features.

Creating temporary connection definition

The simplest way to configure the connection to the MS SQL database at run-time is to build a temporary connection definition:

[crayon-6767930c16ef4574088762/]

In the sample, the temporary definition is created when clicking the Connect button.

Master Details

In the demo database, the Categories and Products tables have one-to-many relations by CategoryID field. The qryCategories.SQL property is set up as follows:

[crayon-6767930c16efb816304773/]

In addition, the qryProducts.SQL property is set up as follows:

[crayon-6767930c16efc409202623/]

Finally, the qryProducts.MasterSource property is set to dsCategories, while the MasterFields property is set to CategoryID. This creates a master-details relationship between the datasets.

Executing queries

Simple query execution is demonstrated via ExecSQL method of TFDConnection.

Inserting a record providing parameters of various types:

[crayon-6767930c16efe742117414/]

Updating records:

[crayon-6767930c16eff549693205/]

Deleting a record:

[crayon-6767930c16f00105033841/]

Getting a scalar value from the database:

[crayon-6767930c16f01417278799/]

If you want to check the original post of this sample, please visit the link below:

http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.MSSQL_Sample

Head over and check out the full source code for connecting to Microsoft SQL Server in a Delphi Windows app.

You may quickly install servers on Windows operating systems using IDE software. This may help you connect to the Microsoft SQL Server, when developing in Delphi and C++ environments. You can request a free demo here.

Exit mobile version