This walkthrough supposes that you know how to connect to server (tutorials "Connecting To SQL Server" and "Connecting To SQL Server Compact"), how to create necessary objects on the server (tutorial "Creating Database Objects"), and how to insert data to created tables (tutorial "Inserting Data Into Tables").
As we know, an original function of any database application is establishing connection to a data source and working with data contained in it. SDAC provides several components that can be used for data retrieving, such as TMSQuery and TMSTable. We will discuss data retrieving using these components.
The goal of this tutorial is to retrieve data from a table dept.
The following code demonstrates retrieving of data from the dept table using the TMSQuery component:
[Delphi]
var
q: TMSQuery;
begin
q := TMSQuery.Create(nil);
try
// con is either TMSConnection or TMSCompactConnection already set up
q.Connection := con;
// retrieve data
q.SQL.Text := 'SELECT * FROM dept';
q.Open;
// shows the number of records obtained from the server
ShowMessage(IntToStr(q.RecordCount));
finally
q.Free;
end;
end;
[C++Builder]
{
TMSQuery* q = new TMSQuery(NULL);
try
{
// con is either TMSConnection or TMSCompactConnection already set up
q->Connection = con;
// retrieve data
q->SQL->Text = "SELECT * FROM dept";
q->Open();
// shows the number of records obtained from the server
ShowMessage(IntToStr(q->RecordCount));
}
__finally
{
q->Free();
}
}
The following code demonstrates retrieving of data from the dept table using the TMSTable component:
[Delphi]
var
tbl: TMSTable;
begin
tbl := TMSTable.Create(nil);
try
// con is either TMSConnection or TMSCompactConnection already set up
tbl.Connection := con;
// retrieve data
tbl.TableName := 'dept';
tbl.Open;
// shows the number of records obtained from the server
ShowMessage(IntToStr(tbl.RecordCount));
finally
tbl.Free;
end;
end;
[C++Builder]
{
TMSTable* tbl = new TMSTable(NULL);
try
{
// con is either TMSConnection or TMSCompactConnection already set up
tbl->Connection = con;
// retrieve data
tbl->TableName = "dept";
tbl->Open();
// shows the number of records obtained from the server
ShowMessage(IntToStr(tbl->RecordCount));
}
__finally
{
tbl->Free();
}
}
It is also possible to use stored procedures for data retrieving. In this case, all data manipulation logic is defined on server. You can find more about using stored procedures in the tutorial "Stored Procedures".