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.
Table of Contents
Location
You can find the MSSQL sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
Object PascalDatabaseFireDACSamplesGetting StartedMSSQL
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the sample repositories according to your RAD Studio version.
How to Use the Sample
- Navigate to the location given above and open
GettingStarted.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
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:
1 2 3 4 5 6 7 8 9 10 11 |
<strong>with</strong> dbMain <strong>do</strong> <strong>begin</strong> Close; <em>// create temporary connection definition</em> <strong>with</strong> Params <strong>do</strong> <strong>begin</strong> Clear; Add('DriverID=MSSQL'); Add('Server=(local)'); Add('Database=FDDemo'); <strong>end</strong>; Open; <strong>end</strong>; |
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:
1 |
select * from Categories |
In addition, the qryProducts.SQL property is set up as follows:
1 2 |
select * from Products where CategoryID = :CategoryID |
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:
1 2 |
dbMain.ExecSQL('insert into Categories(CategoryName, Description, Picture) ' + 'values(:N, :D, :P)', ['New category', 'New descr', $0334]); |
Updating records:
1 2 |
dbMain.ExecSQL('update Products set UnitPrice = UnitPrice * :P1 + :P2 ' + 'where ProductID < 3', [Random(5), Random(3)]); |
Deleting a record:
1 |
dbMain.ExecSQL('delete from Categories where CategoryName like :N', ['New category']); |
Getting a scalar value from the database:
1 |
iID := dbMain.ExecSQLScalar('select MAX(CategoryID) from Categories'); |
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
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.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Code snippets look like this:
with dbMain do begin
Close;
// create temporary connection definition
with Params do begin
Clear;
Add(‘DriverID=MSSQL’);
Add(‘Server=(local)’);
Add(‘Database=FDDemo’);
end;
Open;
end;
Why I see all of that tags instead of normal code?.. (Button “Toggle plain code” does nothing useful.)
Either it is fixed now, or there is an issue with your browser. They are working for me.